SAMANTUS Python for Data Analytics — Complete Training Manual MODULE 02
← Back to Course Index

Python Basics


On This Page

Module 2: Python Basics


2.1  Variables — Naming & Assignment Rules

A variable is simply a named container used to store data in memory so that it can be used and modified later in the program. Unlike some languages, Python does not require you to declare the type of a variable — it figures that out automatically based on the value you assign.

► Creating Variables
name = "Iqubal"
age = 28
course_fee = 15000.50
is_enrolled = True

print(name, age, course_fee, is_enrolled)
Output
Iqubal 28 15000.5 True

Rules for Naming Variables

✗ Common Mistake
Beginners often write 1name = "Raj" which throws a SyntaxError because variable names cannot start with a digit. Always start with a letter or underscore.
► Multiple Assignment
# Assign multiple variables in one line
a, b, c = 10, 20, 30
print(a, b, c)

# Assign the same value to multiple variables
x = y = z = 100
print(x, y, z)
Output
10 20 30
100 100 100

2.2  Data Types in Python

Every value in Python has a data type that determines what kind of operations can be performed on it. Python automatically detects the data type — this is called dynamic typing.

Data TypeExampleDescription
intage = 28Whole numbers, positive or negative
floatprice = 499.99Decimal / floating-point numbers
strname = "Samantus"Text data, enclosed in quotes
boolis_active = TrueOnly two values: True or False
listitems = [1,2,3]Ordered, changeable collection
tuplepoint = (10,20)Ordered, unchangeable collection
dictuser = {"id":1}Key-value pair collection
settags = {1,2,3}Unordered collection of unique items
NoneTyperesult = NoneRepresents "no value"
► Checking Data Types with type()
x = 28
y = 28.5
z = "Samantus"
print(type(x))
print(type(y))
print(type(z))
Output
<class 'int'>
<class 'float'>
<class 'str'>

2.3  Type Casting / Type Conversion

Type casting means converting a value from one data type to another — for example, converting user input (which is always text/string) into a number so you can perform calculations on it.

► Type Casting Examples
a = "25"          # string
b = int(a)        # convert to integer
print(b + 5)      # now math works

c = 9.8
d = int(c)        # float to int (drops decimal)
print(d)

e = 100
f = str(e)        # int to string
print("Fee: " + f)
Output
30
9
Fee: 100
ℹ Note
int() drops the decimal part completely rather than rounding — int(9.8) becomes 9, not 10. Use round() if you need rounding.

2.4  Input and Output

The input() function lets your program take data from the user, and print() displays output back to the screen.

► Taking User Input
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "- you are", age, "years old")
⚠ Important
input() always returns a string, even if the user types a number. You must convert it using int() or float() before doing any math with it.
► Formatted Output using f-strings
name = "Priya"
fee = 15000
print(f"Hi {name}, your course fee is Rs.{fee}")
Output
Hi Priya, your course fee is Rs.15000

2.5  Comments in Python

Comments are notes in your code that Python ignores when running the program. They help you and others understand what the code does.

► Single-line and Multi-line Comments
# This is a single-line comment
print("Hello")   # this comment is at the end of a line

"""
This is a multi-line comment / docstring
often used to explain what a function does
"""

2.6  Keywords in Python

Keywords are reserved words that have special meaning in Python and cannot be used as variable names.

CategoryKeywords
Value keywordsTrue, False, None
Logical operatorsand, or, not
Conditionalsif, elif, else
Loopsfor, while, break, continue, pass
Functions/Classesdef, return, class, lambda
Exception handlingtry, except, finally, raise
Importimport, from, as

2.7  Operators

Operators are special symbols used to perform operations on variables and values.

TypeOperatorsExample
Arithmetic+ - * / // % **10 % 3 = 1
Comparison== != > < >= <=10 > 5 = True
Logicaland or notTrue and False = False
Assignment= += -= *= /=x += 5
Membershipin not in"a" in "cat" = True
Identityis is notx is None
► Operators in Action
a = 10
b = 3
print(a + b)   # 13
print(a % b)   # 1  (remainder)
print(a ** b)  # 1000 (power)
print(a // b)  # 3  (floor division)
Output
13
1
1000
3

2.8  Expressions

An expression is any combination of values, variables, and operators that Python can evaluate to produce a result.

► Expression Examples
price = 500
quantity = 3
discount = 0.10

total = price * quantity * (1 - discount)
print(total)
Output
1350.0
✓ Instructor Tip
Use real business examples like invoice totals, discounts, and GST calculations while teaching expressions — students connect much faster with real numbers than abstract examples.

2.9  Practical Exercises

Basic (5 Questions)

1. Create variables for your name, age, and city, and print them together.

2. Store two numbers in variables and print their sum, difference, and product.

3. Take a number as input from the user and print its data type.

4. Write a program using an f-string to print 'My name is ___ and I am ___ years old'.

5. Add comments explaining each line of one of your earlier programs.

Intermediate (5 Questions)

1. Take two numbers as input (as strings) and correctly convert and add them.

2. Write a program to swap the values of two variables without using a third variable.

3. Create a simple bill calculator: price, quantity, and print the total using an expression.

4. Demonstrate the difference between int(9.9) and round(9.9) with printed output.

5. List 5 Python keywords and explain what each one is used for.

Advanced (5 Questions)

1. Write a program that calculates GST (18%) on a product price and prints the final amount.

2. Explain, with an example, why input() always needs type casting for numeric operations.

3. Create a program using multiple assignment to initialize 4 variables in a single line.

4. Research the difference between is and == in Python and demonstrate with a code example.

5. Build a simple 'Student Fee Calculator' that takes fee, discount %, and prints the final payable fee.

2.10  Module Quiz (MCQs)

Q1. Which of these is a valid variable name?

Q2. What is the data type of True in Python?

Q3. What does int("25") return?

Q4. Which operator gives the remainder of a division?

Q5. What does input() always return by default?

ℹ Answer Key
1-b, 2-c, 3-c, 4-c, 5-c

2.11  Module Assignment

Assignment: Personal Expense Calculator

Expected Output: A working script that takes 3 inputs, correctly type-casts them, computes the total using an expression, and prints a nicely formatted summary line.

2.12  Interview Questions — Module 2


2.13  Student Notes Page

Use this space to write down key points, doubts, and your own examples from today's session.