Variables and Data Types
✕Introduction to Variables
- Containers for storing data values in memory for using later
- Created by assigning a value to a name
- Can be reassigned to different values later
- Constants are variables that should not change
x = 5name = "Rabindra"PI = 3.14159is_student = Truecolors = ["red", "green", "blue"]
Examples:
Variable Naming Conventions
- Should have meaningful names
- Can contain letters (a-z, A-Z), digits (0-9), and underscores (_)
- Should not start with a digit
- Case-sensitive (name and NAME are different variables)
- Reserved keywords cannot be used as variable names. (eg: def, for, while, etc.)
- Constants are usually written in uppercase
- snake_case is preferred in Python
- Invalid examples: my variable, 2nd_variable, user-name, c, for, mark%
- Valid examples: my_variable, variable_2nd, user_name, course_name
Data Types
- Type of value that a variable holds
- Operation on variables depend on their data types
print( 5 + 3 )# Result: 8 (numeric addition)print('5' + '3')# Result: '53' (string concatenation)print( '5' + 3 )# Error: can only concatenate str (not "int") to strprint( 10 / 2 )# Result: 5.0 (numeric division)print('Cow' / 'Dog')# Error: unsupported operand type(s)print( 5 * 3 )# Result: 15 (numeric multiplication)print('5' * 3)# Result: '555' (string repetition)print( '5' * '3' )# Error: can't multiply sequence by non-int of type 'str'- Python data types are broadly classified into: Primitive and Complex data types
Examples:
Primitive Data Types
- int (integer): Whole numbers (+ve, -ve or zero), without decimal point. Eg:
10,-5,0 - float: Numbers with a decimal point (+ve, -ve or zero). Eg:
3.14,-2.5,0.1,3.0 - complex: Numbers with real and imaginary parts. Eg:
3 + 4j,-2.5 + 3.14j,0 + 2j - str: Sequence of characters enclosed in quotes. Eg:
'Python',"Hi","""Hello World""" - bool: Represents truth values. Can be either
TrueorFalse
Numeric Data Types
String Data Type
Boolean Data Type
Complex Data Types
- Collection of items enclosed in square brackets.
- Eg:
[1, 2, 3],["apple", "pulp", "cherry"],[1, "hello", 3.14],[True, 4, "pulp"] - Collection of items enclosed in parentheses.
- Eg:
(1, 2, 3),("apple", "pulp", "cherry"),(1, "hello", 3.14),(True, 4, "pulp") - Collection of unique items enclosed in curly braces.
- Eg:
{1, 2, 3},{"apple", "pulp", "cherry"},{1, "hello", 3.14},{True, 4, "pulp"} - Collection of key-value pairs enclosed in curly braces.
- Eg:
{"name": "Rabindra", "age": 25},{"fruit": "apple", "healthy": True}
List
Tuple
Set
Dictionary
Checking Data Types
- We need to check data type of variable before performing operations
- Data type of variable can be checked using type(variable_name)
num_1 = 10 print(type(num_1))course_name = "Python Basics" print(type(course_name))is_active = True print(type(is_active))colors = ["red", "green", "blue"] print(type(colors))
Examples:
Type Conversion
- We often need to convert data from one type to another for performing appropriate operations
- Python provides several built-in functions for type conversion
- Conversion functions: int( ), float( ), str( ), bool( ), list( ), tuple( ), set( ), dict( )
- If conversion is not possible due to invalid data, Python raises a ValueError
num_str = "100"num_int = int(num_str)# Converts to integernum_float = float(num_str)# Converts to floatnum_bool = bool(num_str)# Converts to booleannum_str = str(num_str)# Converts to string
Examples:
Type Conversion Examples
var_1 = '5' var_2 = '2.5' print(var_1 + var_2)# 52.5print(float(var_1) + float(var_2))# 7.5var_3 = '10' var_4 = 5 print(var_3 * var_4)# 1010101010print(int(var_3) * var_4)# 50
Examples:
