5/4/26, 11:01 AM 4.Basics_python.
ipynb - Colab
keyboard_arrow_down Python Foundations for Machine Learning – Variables, Data Types & Operators
Learning Objectives
By the end of this session, you will be able to:
Create and name variables correctly
Understand and use Python basic data types
Identify and convert data types
keyboard_arrow_down 1. Basic Structure - Printing
print("Hello, World!")
Hello, World!
keyboard_arrow_down 2. Comments
Comments help explain the code.
Anything after # is ignored by Python
Multi-line comments are written using triple quotes ( """ """ )
1. Single-line Comment
# This program prints a welcome message
print("Welcome to Python")
Welcome to Python
2. Multi-line Comment
"""
This program collects student details
and converts marks from string to integer.
It also displays the final result.
"""
student_marks_str = "85"
student_marks = int(student_marks_str)
print(student_marks)
85
keyboard_arrow_down 3. Variables
An identifier containing a known information
Information is referred to as value
Variable name points to a memory address or a storage location and used to reference the stored value
name = "Rahul"
print(name)
Rahul
student_name = "Suja"
marks = 85
print("Student name is",student_name, "and total marks",marks)
Student name is Suja and total marks 85
[Link] 1/6
5/4/26, 11:01 AM 4.Basics_python.ipynb - Colab
keyboard_arrow_down Rules for Variable Nomenclature
Variables can be named alphanumerically
Usage of special characters other than _ (underscore) throws an error
The name of a variable should always start with an alphabet
Descriptive variable names - eg. student_marks
Avoid one character variables except loops
Do not use spaces
# Examples
customer_age = 25
annual_salary = 50000
is_graduate = True
print(customer_age)
print(annual_salary)
print(is_graduate)
25
50000
True
student1 = "Rahul"
student-name = "Rahul"
File "/tmp/ipykernel_751/[Link]", line 1
student-name = "Rahul"
^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
student name = "Rahul"
File "/tmp/ipykernel_751/[Link]", line 1
student name = "Rahul"
^
SyntaxError: invalid syntax
keyboard_arrow_down What are Keywords?
Keywords are reserved words in Python.
They already have a special meaning
We cannot use them as variable names
Examples of Keywords
if , else , for , while , class , def , return , True , False
Error Scenario
if = 10
File "/tmp/ipykernel_1375/[Link]", line 1
if = 10
^
SyntaxError: invalid syntax
import keyword
print([Link])
print(len([Link]))
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else
35
keyboard_arrow_down Built-in Names (Avoid Using)
[Link] 2/6
5/4/26, 11:01 AM 4.Basics_python.ipynb - Colab
These are already defined functions in Python.
Examples: sum() , type() , list() , int()
numbers = [10, 20, 30]
print(sum(numbers))
60
sum = 100
# Bad practice
sum = 100
sum
print(sum(numbers)) # Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_1375/[Link] in <cell line: 0>()
----> 1 print(sum(numbers)) # Error
TypeError: 'int' object is not callable
[Link]
keyboard_arrow_down Assigning Multiple Variables
Python allows assigning multiple variables in one line.
Example:
Physics, Chemistry, Mathematics = 89, 90, 75
print(Physics)
print(Chemistry)
print(Mathematics)
89
90
75
Physics, Chemistry = 89, 90, 75
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_1375/[Link] in <cell line: 0>()
----> 1 Physics, Chemistry = 89, 90, 75
ValueError: too many values to unpack (expected 2)
Error Scenario: If the number of variables on the left-hand side doesn't match the number of values on the right, it will raise a
ValueError.
keyboard_arrow_down 5. Basic Data Types
Basic data types Description Values Representation
Boolean represents two values of logic and associated with conditional statements True and False bool
Integer positive and negative whole numbers set of all integers, Z int
Complex contains real and imaginary part (a + jb ) set of complex numbers complex
Float real numbers floating point numbers ∈ R float
String Sequence of characters (digits, alphabets, and special characters) Multiple characters enclosed in a pair of quotations str
age = 25 # int
height = 5.9 # float
is_employed = True # bool
name = "John" # str
[Link] 3/6
5/4/26, 11:01 AM 4.Basics_python.ipynb - Colab
print(age)
print(height)
print(is_employed)
print(name)
25
5.9
True
John
Identifying Data Type
We can identify the data type using type().
print(type(age))
<class 'int'>
print(type(age))
print(type(height))
print(type(is_employed))
print(type(name))
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
keyboard_arrow_down 6. Type Coercion
Coercion means converting one data type into another.
Python supports both implicit and explicit type conversion.
Implicit conversion: Python automatically converts one data type to another when needed.
Explicit conversion: The programmer manually converts data types using functions like int() , float() , or str() .
While Python handles many conversions automatically, some situations:
may lead to errors (e.g., incompatible types), or
require explicit intervention from the programmer.
1. Implicit Type Coercion (Automatic Conversion)
Python will often perform type coercion when it can implicitly convert one data type to another, like when adding an integer to a float.
result = 5 + 3.5
print(result)
8.5
print(type(result))
<class 'float'>
Implicit coercion failure
When you try to add a string and a number directly, Python will raise a TypeError, since it cannot implicitly convert between them.
However, you can perform explicit type conversion.
age = "25" # string
salary = 50000 # integer
# This will raise a TypeError
# print(age + salary) # TypeError: can only concatenate str (not "int") to str
print(type(age))
<class 'str'>
print(age + salary)
[Link] 4/6
5/4/26, 11:01 AM 4.Basics_python.ipynb - Colab
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_1375/[Link] in <cell line: 0>()
----> 1 print(age + salary)
TypeError: can only concatenate str (not "int") to str
2. Explicit Type Coercion (Using Type Conversion Functions)
You can manually convert types using Python's built-in functions like int(), float(), str(), etc.
Explicit coercion from string to integer
salary = "45000" # comes as string
print(type(salary))
<class 'str'>
salary = int(salary)
print(type(salary))
<class 'int'>
Explicit coercion from float to integer
product_price = 9.7 # price with decimal
product_price_whole = int(product_price) # converted to whole number
print(product_price_whole)
print(type(product_price_whole))
9
<class 'int'>
Coercion failure
If you try to coerce a string that contains non-numeric characters into an integer, it will raise a ValueError.
salary = "forty-two" # string with non-numeric characters
print(int(salary))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_1375/[Link] in <cell line: 0>()
1 salary = "forty-two" # string with non-numeric characters
2
----> 3 print(int(salary))
ValueError: invalid literal for int() with base 10: 'forty-two'
What is input()?
input() is used to get data from the user (keyboard)
user_name = input("Enter your name: ")
print(user_name)
Enter your name: John
John
type(user_name)
str
END
[Link] 5/6
5/4/26, 11:01 AM 4.Basics_python.ipynb - Colab
[Link] 6/6