Variables and Data Types
โ1. Variables
- Imagine your room has different containers: ๐ฆ Box labeled Books ๐ Box labeled Clothes ๐ Basket labeled Fruits Each container stores something and has a name so that you can find it later. Similarly, a Variable is a named container used to store data in a computer's memory for later use. ๐ฆ name โ "Rabindra" ๐ฆ age โ 25 ๐ฆ PI โ 3.14159
1.1 Real World Scenario

- A variable is a named storage location used to store data values that can be used or modified later in a program.
- โ Created by assigning a value to a name โ Can be reassigned to different values later โ Used to store information for future use โ Makes programs easier to understand and maintain
x = 5name = "Rabindra"PI = 3.14159is_student = Truecolors = ["red", "green", "blue"]- Sometimes we have values that should not change throughout a program. These values are called Constants.
Examples:
PI = 3.14159MAX_MARKS = 100๐ Constants are usually written in UPPERCASE letters.
1.2 Definition
1.3 Key Points
1.4 Examples:
1.5 Constants

2. Variable Naming Conventions
- โ Should have meaningful names โ Can contain letters (a-z, A-Z), digits (0-9), and underscores (_) โ Must start with a letter or underscore (_) โ Python is case-sensitive โ snake_case is preferred โ this means writing variable names in lowercase with underscores between words, like total_marks โ Reserved keywords cannot be used as variable names (for, while, def, etc.)
2.1 Rules

- name = "Ram" NAME = "Hari" Here, name and NAME are treated as two different variables due to case sensitivity.
2.2 Case Sensitivity
3. Data Types
- Consider the following examples:
print(5 + 3)# โ 8print("5" + "3")# โ "53"print("5" + 3)# โ Error ๐ค Why are the results different? Because the values have different Data Types. - A data type specifies the type of value stored in a variable and determines the operations that can be performed on it.
- Python data types are broadly classified into: ๐น Primitive Data Types โ Store a single value (like a number or word) ๐น Collection Data Types โ Store multiple values together (like a list of items)
3.1 Why Do We Need Data Types?
3.2 Definition
3.3 Python Data Types

4. Primitive Data Types
- Whole numbers without decimal points.
Examples:
10,-5,0Example Variable:age = 25 - Numbers with decimal points.
Examples:
3.14,-2.5,0.1,3.0Example Variable:temperature = 36.59 - Sequence of characters enclosed in quotes.
Examples:
'Python',"Hi","""Hello World""","3.0"Example Variable:name = "Alice" - Represents truth values.
Possible values:
True,FalseExample Variable:is_active = True - Numbers with real and imaginary parts.
Examples:
3 + 4j,-2.5 + 3.14j,0 + 2jExample Variable:z = 2 + 3j
4.1 ๐ข Integer (int)
4.2 ๐ข Float (float)
4.3 ๐ค String (str)
4.4 โ
Boolean (bool)
4.5 Complex (complex)
5. Collection Data Types
- A collection of items enclosed in square brackets [ ].
Examples:
[1, 2, 3],["apple", "banana", "cherry"],[1, "hello", 3.14]Example Variable:my_list = [True, 4, "pulp"] - A collection of items enclosed in parentheses ( ).
Examples:
(1, 2, 3),("apple", "banana", "cherry"),(1, "hello", 3.14)Example Variable:my_tuple = (True, 4, "pulp") - A collection of unique items enclosed in curly braces { }.
Examples:
{1, 2, 3},{"apple", "banana", "cherry"},{1, "hello", 3.14}Example Variable:my_set = {True, 4, "pulp"} - A collection of key-value pairs enclosed in curly braces { }.
Examples:
{"name": "Alice", "age": 25},{"fruit": "apple", "healthy": True}Example Variable:my_dict = {"city": "Kathmandu", "country": "Nepal"}
5.1 List
5.2 Tuple
5.3 Set
5.4 Dictionary
6. Data Types Affect Operations
print(5 + 3)# โ 8 (int + int = int)print(3.5 + 2.5)# โ 6.0 (float + float = float)print("Hello" + " " + "World")# โ "Hello World" (str + str = str)print("5" + "3")# โ "53" (str + str = str)print(5 + "3")# โ Error (int + str = TypeError)
6.1 Addition โ

print(5 - 3)# โ 2 (int - int = int)print(3.5 - 2.5)# โ 1.0 (float - float = float)print("Hello" - "World")# โ Error (str - str = TypeError)print(5 - "3")# โ Error (int - str = TypeError)
6.2 Subtraction โ

print(5 * 3)# โ 15 (int * int = int)print(3.5 * 2)# โ 7.0 (float * int = float)print("Hello" * 3)# โ "HelloHelloHello" (str * int = str)print("Hello" * "World")# โ Error (str * str = TypeError)
6.3 Multiplication โ๏ธ

print(5 / 2)# โ 2.5 (int / int = float)print(3.5 / 2)# โ 1.75 (float / int = float)print("Hello" / 2)# โ Error (str / int = TypeError)print(5 / "2")# โ Error (int / str = TypeError) ๐ Same operator can behave differently depending on the data type. ๐ TypeError means Python doesn't allow that operation between these two data types.
6.4 Division โ

7. Checking Data Types
- We often need to know the data type of a variable before performing operations because the result depends on the data type.
- We can check the data type of a variable using the type() function.
Syntax:
type(variable_name)Example:num = 10print(type(num))# โ <class 'int'>name = "Alice"print(type(name))# โ <class 'str'>is_active = Trueprint(type(is_active))# โ <class 'bool'>colors = ["red", "green", "blue"]print(type(colors))# โ <class 'list'>
7.1 Why Check Data Types?
7.2 How to Check Data Types?

8. Type Conversion
- Sometimes we need to convert data from one type to another before performing operations. For example, what if we want to add two numbers, but they are stored as strings?
Example:
num_1 = "10" num_2 = "5" print(num_1 + num_2)# โ "105" (str + str = str)print(int(num_1) + int(num_2))# โ 15 (int + int = int) To convert a value, write the new data type's name followed by the value in brackets, like this: target_datatype(value) Some common conversions are: int() โ Converts to integer float() โ Converts to float str() โ Converts to string bool() โ Converts to boolean list() โ Converts to list tuple() โ Converts to tuple set() โ Converts to set dict() โ Converts to dictionary
8.1 Why Do We Need Type Conversion?

num_str = "100" print(type(num_str)) num_int = int(num_str)# โ 100print(type(num_int))# โ <class 'int'>num_float = float(num_str)# โ 100.0print(type(num_float))# โ <class 'float'>num_bool = bool(num_str)# โ Trueprint(type(num_bool))# โ <class 'bool'>new_num = 10.0 print(type(new_num)) new_num_str = str(new_num)# โ "10.0"print(type(new_num_str))# โ <class 'str'> โ ๏ธ If conversion is not possible due to invalid data, Python raises a ValueError. For example, int("apple") will raise ValueError because "apple" cannot be converted into an integer.
8.2 Example of Type Conversion
9. Type Conversion Practical Examples
var_1 = "5" var_2 = "2.5" print(var_1 + var_2)# โ 52.5var_1_float = float(var_1) var_2_float = float(var_2) print(var_1_float + var_2_float)# โ 7.5var_5 = "10" var_6 = 5 print(var_5 * var_6)# โ 1010101010print(int(var_5) * var_6)# โ 50
9.1 Example 1
9.2 Example 2
Practice QuestionsNot started
Inside python_lab folder, create a new folder named variable. In the variable folder, solve each questions in a separate file.
Question 1 of 2
- ๐ชช Create a variable to store your name and print it.
- ๐ Create a variable to store your age using proper data type and print it.
- ๐ Create a constant named PI to store the value 3.14159 and print it.
- ๐ณ๐ต Create a variable to store a boolean value
is_nepali(True or False, like answering a yes/no question) and print it.
Inside python_lab folder, create a new folder named data_type_conversion. In the data_type_conversion folder, solve each questions in a separate file.
Question 2 of 2
- ๐ You store your age as
num_int = 15. Display its data type. Now convert it to float and string, and display the type again each time. - ๐ A shop stores item price as text:
num_str = "25". Add a delivery charge of 10 to it after proper type conversion, and print the total. - ๐ก๏ธ A thermometer reads
float_num = 10.5. Convert it to string, then add"10"(a label) to it, and print the result โ what happens? - โ
A form field stores
is_active = Truefor "is this user active?". Convert it to string and print both the result and its data type. - ๐ฐ Two friends write down money they have as text:
num_1 = "5"andnum_2 = "2.5". Convert both to float and print their total. - ๐งพ A cashier accidentally writes :
print(5 + "5")โ predict what this will print, then run it and check. Now fix the code so it correctly prints 10.
- ๐ You store your age as
