Skip to main content

Python Fundamental (int,float,str,bool) Data types Explained with Examples


Python Fundamental (int,float,str,bool) Data types Explained with Examples

What are Data Types?

Data types define the type of value that can be stored in a variable. Every value in Python belongs to a specific data type, and each data type behaves differently.

Examples of Python Data Types:
  1. int → Integer (Whole numbers)
  2. float → Decimal numbers
  3. bool → True or False
  4. str → Text/String
  5. list → Ordered collection (Mutable means Changing the Values)
  6. tuple → Ordered collection (Immutable means doestn't Changed the Values)
  7. dict → Key-value pairs (Mutable)
  8. set → Unique unordered values (Mutable)

Example: Student Information (All Data Types)

Python

name="Hamid Hasnain"  # ----> is Called String
age=19            # ----> is Called intgers(int)
percentage=51.55  # ----> is Called Float
you_pass=True    # ----> is Called Boolean(bool)
# list[]
subject_name=["Physics","Chemistry","Biology","Mathamethics","Urdu","English"]
# tuple()
Grad_subject=("A","A","B","C","C")
#set{}
skills={"Python","C","HTML","CSS","Design"}
# Dictionary(dict) {keys:valus} pairs
Dict={
    "name":"Hamid",
    "age":19,
    "rool_no":1,
    }
    
print("Student Information Portfolio")
print(name)
print(age)
print(percentage)
print(subject_name)
print(Grad_subject)
print(skills)
print(Dict)

Output (show in Terminal):

Terminal
Student Information Portfolio
Hamid Hasnain
19
51.55
['Physics', 'Chemistry', 'Biology', 'Mathamethics', 'Urdu', 'English']
('A', 'A', 'B', 'C', 'C')
{'Python', 'C', 'HTML', 'Design', 'CSS'}
{'name': 'Hamid', 'age': 19, 'rool_no': 1}

Student Information Portfolio – Code Explanation

1. String (str)

name = "Hamid Hasnain"

name is a variable.
"Hamid Hasnain" is text, so its data type is String (str).
A string is always written inside single (' ') or double (" ") quotes.

2. Integer (int)

age = 19

age stores the value 19.
It is a whole number (without a decimal point).
Therefore, its data type is Integer (int).

3. Float (float)

percentage = 51.55

percentage stores the value 51.55.
A number with a decimal point is called a Float.
Therefore, its data type is Float (float).

4. Boolean (bool)

you_pass = True

A Boolean data type has only two values: True and False
Here, True means the student has passed.
Therefore, its data type is Boolean (bool).

5. List (list)

subject_name = ["Physics", "Chemistry", "Biology", "Mathematics", "Urdu", "English"]

A List is used to store multiple values.
It is written inside square brackets [ ].
A List is Mutable, which means its values can be added, removed, or changed after creation.

6. Tuple (tuple)

Grad_subject = ("A", "A", "B", "C", "C")

A Tuple also stores multiple values.
It is written inside parentheses ( ).
A Tuple is Immutable, which means its values cannot be changed after it is created.

7. Set (set)

skills = {"Python", "C", "HTML", "CSS", "Design"}

A Set is written inside curly braces { }.
It stores unique values only (duplicate values are not allowed).
A Set is Mutable, so values can be added or removed.

8. Dictionary (dict)

Dict = {
"name": "Hamid",
"age": 19,
"roll_no": 1,
}

A Dictionary stores data as key-value pairs.
It is written inside curly braces { }.
Example:
"name" → Key
"Hamid" → Value
A Dictionary is Mutable, so keys and values can be updated.

Why are Data Types Important?

Data types are very important because not all data is the same. Python needs to know what kind of data it is working with so it can perform the correct operations.

Benefits of Data Types

Store different kinds of data.
Perform the correct operations.
Reduce programming errors.
Make code easier to read and understand.
Improve program performance.

Summary

Data types define what kind of value a variable stores.
Python has many built-in data types such as int, float, bool, str, list, tuple, dict, and set.
Data types help Python perform the correct operations and make programs more efficient.
Mutable data types can be changed after creation (list, dict, set).
Immutable data types cannot be changed after creation (int, float, bool, str, tuple).

What are Fundamental Data Types

Fundamental Data Types are the basic types of data in Python. They tell us what kind of value is stored, such as an integer, string, float, or Boolean.

Integer (int)

Integer (int) is one of the fundamental data types in Python. It is used to store whole numbers without decimal values. Examples are 0, 1, 2, 3, 100, and -50. In Python, the integer data type is written as int.

Example

Python

x=2
print(x)
print(type(x))
y=3
print(y)
print(type(y)

Output (show in Terminal):

Terminal
2
(class int)
3
(class int)

type( ) Function

type ( ) is a built-in function in Python that is used to find a type of data types of variable such as integer , float , boolean , string or onother data type

Syntax

Syntax
print(type(variable_name))

Float (float)

float is a fundamentel data type in Python. It is used to store decimal (floating-point) numbers.for Example: 0.5 , 1.0,99.99,...etc.In Python, the float data type is represented by float.

Example

Python

x=1.5
print(x)
print(type(x))
y=99.99
print(y)
print(type(y))

Output (show in Terminal):

Terminal
1.5
(class float)
99.99
(class float)

String (str)

String (str) is a fundamental data type in Python. It is used to store a collection of characters or text. A string is written inside double quotes (" ") or single quotes (' ').
Examples of string values: "Hello", 'Python', "Hamid", "123"
In Python, the keyword (data type) for a string is str

Example

Python

# string 
name="Nicein Python Learn" 
print("Company Name:",name)
# Check a data types of name variable using type( ) function 
print("the data types of variable is:",type(name))


Output (show in Terminal):

Terminal
Company Name:Nicein Python Learn
the data types of variable is:(class str)

Boolean (bool)

Boolean (bool) Boolean (bool) is a fundamental data type in Python. It is used to store only two values: True or False. It is commonly used to represent yes/no, on/off, or true/false conditions. Examples of Boolean values: True, False In Python, the Boolean data type is represented by bool.

Example

Python

#bool example
line="I Love Python"
print("Python" in line) 
print(type(line))
print("C++" in line)

Explanation: The word "Python" is present in the string "I Love Python", so the output is True.
The word "C++" is does not present in the string "I Love Python", so the output is False.

Output (show in Terminal):

Terminal
True
(class bool)
False

Printing a String with a Variable

Sometimes we want to print a message and a variable together. In Python, we can do this by using a comma (,) or an f-string.

Example: Using Comma(,)

Python

name="Hamid"
age=19
print("My Name is:",name)
print("my Age is:",age)
# or
print("My Name is:", name ,"and Age is:", age)

Output (show in Terminal):

Terminal
My Name is: Hamid
My Age is:19
My Name is: Hamid and Age is:19

f-String in Python

An f-string (formatted string) is used to print messages and variables together in a simple and readable way.
To create an f-string, write the letter f before the opening double quotes (" ") or single quotes (' '). Then, write your message (optional) and place the variable inside curly braces ({ }).

Syntax
print(f"Message {variable}") 

Example: f-string

Python

name="Hamid"
age=19
print(f"My Name is {name}.")
print(f"My Age is {age}.")
# or
print(f(My Name is {name} and Age is {age} Year olds.")

Output (show in Terminal):

Terminal
My Name is Hamid.
My Age is 19.
My Name is Hamid and Age is 19 Year olds. 

Advantages and Disadvantages of Comma/f-string

Advantages of Comma (,)

✅ Easy for beginners to learn.
✅ No need to convert int or float into str.
✅ Good for simple print() statements.

Disadvantages of Comma (,)

❌ Output formatting is less flexible.
❌ Code becomes harder to read when many variables are printed.
❌ Cannot easily control the position of variables inside a sentence.

Advantages of f-String

✅ Easy to read and write.
✅ Variables are inserted directly inside the string.
✅ Best for creating formatted messages.
✅ Faster and more modern than older formatting methods.
✅ Recommended in Python 3.6 and later.

Disadvantages of f-String

❌ Available only in Python 3.6 or later.
❌ Beginners may forget to use f before the quotes.
❌ Beginners may forget to put variables inside { }.

Python Practice Questions – Fundamental Data Types (int, float, str, bool)

Question Practice Tips 📝 Try to solve the question yourself → ✅ Check your answer → 🤖 Use ChatGPT or Gemini AI to verify whether your answer is correct or incorrect. If your answer is wrong or you don't understand the question, review the concept and try solving it again on your own. Keep practicing until you can solve it without help.

Easy:

1) Create an integer variable to store a student's age and print it.
2) Create a float variable to store the price of a product and print it.
3) Create a string variable to store your name and print a welcome message.

Medium:

4) Create integer variables for the marks of three subjects and calculate the total marks.
5) Create float variables for the prices of three products and calculate the total bill.
6) Create string variables for a person's name, city, and profession, then print a complete introduction.
7) Create a boolean variable that stores whether a person is eligible to vote based on their age.

Hard:

8) Create variables to store a student's name (str), roll number (int), percentage (float), and pass status (bool). Print all the details.
9) Create variables for an employee's name, monthly salary (float), bonus (float), and employment status (bool). Calculate and print the total salary.
10) Create variables for a customer's name (str), product quantity (int), product price (float), and order status (bool). Calculate the total bill and print all the information.

🚗 Mini Project: Car Information System

Objective:
Create a program to store and display information about a car using the fundamental data types (int, float, str, and bool).
Requirements:
Create variables to store the following information:
1.Car Brand
2.Car Model
3.Manufacturing Year
4.Engine Capacity
5.Fuel Type
6.Color
7.Seating Capacity
8.Price
9.Registration Number
10.Is Available
Expected Output:
Display all the car information in a clear and well-formatted manner.

Comments

Popular posts from this blog

Python Variables and Data Types Explained with Examples

🚀 Welcome to NiceinPythonlearn | 📘 Learn Python with Easy Tutorials, Notes & Practice Questions | 💻 Build Real-World Projects | 💬 Enjoying the content?Share your feedback! | 📝 If you have any questions, suggestions, or face any problems, please contact us using the Contact Form or leave a comment. We will do our best to solve it together. 🤝 | ⭐ Follow us for new updates and keep learning! Introduction Variables and Data Types are the fundamental concepts of Python programming. Variables are used to store data, while Data Types define the kind of data a variable can hold. These concepts form the foundation of every Python program, making code simple, organized, efficient, and easy to understand and maintain. Roman English: Variables data store karte hain, aur Data Types batate hain data kis type ka hai. Ye Python programming ki basic foundation hain. Learning Objectives After completing this topic, you will be able to: Underst...

Python input and output (I/O) Statements Explained with Examples

Introduction Python Input and Output are the foundation of interactive programming. Input allows users to enter information, while output displays results on the screen. By learning these concepts, you can create programs that communicate with users, solve real-world problems, and build applications that respond to user input effectively. Hinglish:Python Input aur Output interactive programming ki foundation hai. Input ka use karke user se information li jaati hai, aur Output ka use karke screen par result dikhaya jaata hai. In concepts ko seekhne ke baad aap aise programs bana sakte ho jo users se communicate karein, real-world problems solve karein, aur user ke input ke hisaab se response dene wale applications develop karein. Learning Objectives After completing this lesson, you will be able to: 1.Understand the concept of Input and Output in Python. 2.Use the input() function to take data from users. 3.Use the print() function to display info...

Python Print Statement and Comments Explained with Examples

1) Print Statement Function in Python A print statement Function is used to show output on the screen. In Python, we write print() with a small letter p, not a capital letter. Roman : Print statement Function screen par output dikhane ke liye use hota hai. Python me print() small letter p se likhte hain, kyunki Python case-sensitive hota hai. Example of Print Statement Function in Python : print("Hello World") Output(show on Terminal): Hello World Another Example of Print Statement Function in Python : print(10) print(2.0) print(True) print(10+5) print(2*5) print(0-0) Output(show on Terminal): 10 2.0 True 15 10 0 2) Comments in Python (Single and Multiples): Single Line Comments using Hash(#) in Python A comment is used to explain a line of code. In Python, a comment is written using #(hash...