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
  • Examples:
    1. x = 5
    2. name = "Rabindra"
    3. PI = 3.14159
    4. is_student = True
    5. colors = ["red", "green", "blue"]

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
  • Examples:
    1. 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 str
    2. print( 10 / 2 ) # Result: 5.0 (numeric division) print('Cow' / 'Dog') # Error: unsupported operand type(s)
    3. 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

Primitive Data Types

    Numeric Data Types
    1. int (integer): Whole numbers (+ve, -ve or zero), without decimal point. Eg: 10, -5, 0
    2. float: Numbers with a decimal point (+ve, -ve or zero). Eg: 3.14, -2.5, 0.1, 3.0
    3. complex: Numbers with real and imaginary parts. Eg: 3 + 4j, -2.5 + 3.14j, 0 + 2j
    String Data Type
    1. str: Sequence of characters enclosed in quotes. Eg: 'Python', "Hi", """Hello World"""
    Boolean Data Type
    1. bool: Represents truth values. Can be either True or False

Complex Data Types

    List
    1. Collection of items enclosed in square brackets.
    2. Eg: [1, 2, 3], ["apple", "pulp", "cherry"], [1, "hello", 3.14], [True, 4, "pulp"]
    Tuple
    1. Collection of items enclosed in parentheses.
    2. Eg: (1, 2, 3), ("apple", "pulp", "cherry"), (1, "hello", 3.14), (True, 4, "pulp")
    Set
    1. Collection of unique items enclosed in curly braces.
    2. Eg: {1, 2, 3}, {"apple", "pulp", "cherry"}, {1, "hello", 3.14}, {True, 4, "pulp"}
    Dictionary
    1. Collection of key-value pairs enclosed in curly braces.
    2. Eg: {"name": "Rabindra", "age": 25}, {"fruit": "apple", "healthy": True}

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)
  • Examples:
    1. num_1 = 10 print(type(num_1))
    2. course_name = "Python Basics" print(type(course_name))
    3. is_active = True print(type(is_active))
    4. colors = ["red", "green", "blue"] print(type(colors))

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
  • Examples:
    1. num_str = "100"
    2. num_int = int(num_str) # Converts to integer num_float = float(num_str) # Converts to float num_bool = bool(num_str) # Converts to boolean num_str = str(num_str) # Converts to string

Type Conversion Examples

    Examples:
    1. var_1 = '5' var_2 = '2.5' print(var_1 + var_2) # 52.5 print(float(var_1) + float(var_2)) # 7.5
    2. var_3 = '10' var_4 = 5 print(var_3 * var_4) # 1010101010 print(int(var_3) * var_4) # 50