01. Introduction to Python
Python is a popular programming language used in web development, automation, data analysis, artificial intelligence, and cybersecurity.
Python is beginner-friendly because its syntax is easy to read and understand.
Your First Python Program
The print() function displays
information on the screen.
print("Hello, World!")
Hello, World!
Python Comments
Comments help programmers explain their code.
Python ignores comments while executing a program.
A single-line comment starts with #.
# This is a comment
print("Learning Python")
Learning Python
Python Indentation
Indentation means adding spaces at the beginning
of a line. Python uses indentation to group
statements inside blocks such as
if statements and loops.
if 5 > 2:
print("Five is greater")
Five is greater
Practice Questions — Python Basics
Question 1: Write a Python program that displays your name.
Show Solution
print("My name is Alex")
My name is Alex
Question 2: Write a program that prints two messages on separate lines.
Show Solution
print("Hello")
print("Welcome to Python")
Hello Welcome to Python
02. Python Variables
A variable is a name that refers to a value. Variables allow us to store and use information inside a program.
Python does not require us to declare a variable's data type before assigning a value.
Example
name = "John" age = 20 print(name) print(age)
John 20
Changing Variable Values
A variable can be assigned a new value. The latest assigned value replaces its previous value.
age = 20 age = 25 print(age)
25
Variable Naming Rules
- Names can contain letters, numbers, and underscores.
- Names cannot start with a number.
- Names are case-sensitive.
- Python keywords cannot be used as variable names.
student_name is valid,
but 2student is invalid.
Practice Questions — Variables
Question 1: Create variables for your name, age, and city. Print all three.
Show Solution
name = "Alex" age = 21 city = "Delhi" print(name) print(age) print(city)
Alex 21 Delhi
Question 2: Create a variable with value 10, change it to 50, and print it.
Show Solution
number = 10 number = 50 print(number)
50
03. Python Data Types
A data type tells us what kind of value a variable contains. Python supports different types of data, including numbers, text, collections, and Boolean values.
Common Python Data Types
| Data Type | Example | Purpose |
|---|---|---|
| int | 10 | Whole numbers |
| float | 10.5 | Decimal numbers |
| complex | 2 + 3j | Complex numbers |
| str | "Hello" | Text |
| bool | True | True or False |
| list | [1, 2, 3] | Ordered, changeable collection |
| tuple | (1, 2, 3) | Ordered, unchangeable collection |
| dict | {"name": "Alex"} | Key-value pairs |
| set | {1, 2, 3} | Collection of unique elements |
| NoneType | None | Represents no value |
Using type()
The type() function tells us
the data type of a value.
name = "Alex" age = 20 percentage = 95.8 is_student = True print(type(name)) print(type(age)) print(type(percentage)) print(type(is_student))
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
List, Tuple, Set, and Dictionary
fruits = ["apple", "banana"]
numbers = (10, 20)
unique = {1, 2, 3}
student = {"name": "Alex", "age": 20}
print(fruits)
print(numbers)
print(sorted(unique))
print(student)
['apple', 'banana']
(10, 20)
[1, 2, 3]
{'name': 'Alex', 'age': 20}
Practice Questions — Data Types
Question 1: Create an integer, float, and string. Display their types.
Show Solution
a = 10 b = 5.5 c = "Python" print(type(a)) print(type(b)) print(type(c))
<class 'int'> <class 'float'> <class 'str'>
Question 2: Create a list containing three fruits and print it.
Show Solution
fruits = ["apple", "mango", "banana"] print(fruits)
['apple', 'mango', 'banana']
04. Type Conversion in Python
Type conversion means changing a value from one data type to another.
For example, we can convert a string
containing digits into an integer using
int().
Example
number = "123" print(type(number)) number = int(number) print(type(number)) print(number + 10)
<class 'str'> <class 'int'> 133
Common Conversion Functions
int()converts a suitable value to an integer.float()converts a suitable value to a floating-point number.str()converts a value to a string.
int("hello") causes an error
because "hello" is not a valid integer.
Practice Questions — Type Conversion
Question 1: Convert the string "50" into an integer and add 25.
Show Solution
number = "50" number = int(number) print(number + 25)
75
Question 2: Convert 12 into a string and display its type.
Show Solution
number = 12 text = str(number) print(type(text))
<class 'str'>
05. Input and Output in Python
The input() function accepts
information entered by the user.
The print() function displays
information on the screen.
Taking User Input
name = input("Enter your name: ")
print("Hello", name)
Enter your name: Alex Hello Alex
Taking Number Input
The input() function returns
a string. We use int() when
we need to convert valid whole-number input
into an integer.
num1 = int(input("First number: "))
num2 = int(input("Second number: "))
print("Sum:", num1 + num2)
First number: 10 Second number: 20 Sum: 30
Using sep in print()
The sep parameter controls
the separator between values displayed
by print().
print(1, 2, 3, sep="-")
1-2-3
Practice Questions — Input and Output
Question 1: Ask the user for their name and display a welcome message.
Show Solution
name = input("Enter name: ")
print("Welcome", name)
Enter name: Alex Welcome Alex
Question 2: Ask the user for two numbers and display their multiplication.
Show Solution
a = int(input("First: "))
b = int(input("Second: "))
print("Result:", a * b)
First: 5 Second: 4 Result: 20
06. Arithmetic Operators
Arithmetic operators perform mathematical calculations such as addition, subtraction, multiplication, and division.
| Operator | Purpose |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| // | Floor division |
| % | Remainder |
| ** | Exponentiation |
Example
a = 10 b = 3 print(a + b) print(a - b) print(a * b) print(a / b) print(a // b) print(a % b) print(a ** b)
13 7 30 3.3333333333333335 3 1 1000
/ performs normal division,
// performs floor division,
and % returns the remainder.
Practice Questions — Arithmetic
Question 1: Find the remainder when 25 is divided by 4.
Show Solution
print(25 % 4)
1
Question 2: Calculate 5 raised to the power of 3.
Show Solution
print(5 ** 3)
125
07. Assignment and Comparison Operators
Assignment Operators
Assignment operators assign or update values stored in variables.
number = 10 number += 5 print(number) number *= 2 print(number)
15 30
number += 5 means
number = number + 5.
Comparison Operators
Comparison operators compare values
and return True or
False.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
a = 10 b = 20 print(a == b) print(a != b) print(a < b)
False True True
Practice Questions — Assignment and Comparison
Question 1: Start with 20, subtract 5 using an assignment operator, and print the result.
Show Solution
number = 20 number -= 5 print(number)
15
Question 2: Check whether 50 is greater than 30.
Show Solution
print(50 > 30)
True
08. Logical, Identity, and Membership Operators
Logical Operators
Logical operators combine or reverse conditions.
andreturns True when both Boolean conditions are True.orreturns True when at least one Boolean condition is True.notreverses a Boolean result.
a = True b = False print(a and b) print(a or b) print(not a)
False True False
Identity Operators
Identity operators check whether two variables refer to the same object.
a = [1, 2] b = a c = [1, 2] print(a is b) print(a is c) print(a == c)
True False True
Here, a and b
refer to the same list. The list stored in
c has equal contents,
but it is a different object.
Membership Operators
Membership operators check whether a value is present in a collection.
fruits = ["apple", "banana", "mango"]
print("banana" in fruits)
print("grape" not in fruits)
True True
Practice Questions — Logical and Membership
Question 1: Check whether 10 is greater than 5 AND less than 20.
Show Solution
number = 10 print(number > 5 and number < 20)
True
Question 2: Check whether "Python" is present in a list of programming languages.
Show Solution
languages = ["Python", "Java", "C"]
print("Python" in languages)
True
09. Bitwise Operators and Binary Numbers
Computers represent information using
binary digits called bits.
A bit can have the value
0 or 1.
Binary to Decimal
Binary uses base 2, while decimal uses base 10.
For example, binary 10110
equals decimal 22.
number = 0b10110 print(number) print(bin(22))
22 0b10110
Bitwise Operators
Bitwise operators work on the binary representation of integers.
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left shift |
| >> | Right shift |
Example
a = 5 b = 3 print(a & b) print(a | b) print(a ^ b) print(~a) print(a << 1) print(a >> 1)
1 7 6 -6 10 2
~n = -n - 1.
Therefore, ~5 equals
-6.
Practice Questions — Binary and Bitwise
Question 1: Convert decimal 10 into binary using Python.
Show Solution
print(bin(10))
0b1010
Question 2: Find the result of shifting 7 left by one bit.
Show Solution
print(7 << 1)
14
10. Beginner Python Projects
Now use the concepts you have learned to create small Python programs.
Project 1 — Sum of Two Numbers
This program asks the user for two numbers and displays their sum.
num1 = int(input("First number: "))
num2 = int(input("Second number: "))
result = num1 + num2
print("Sum:", result)
First number: 10 Second number: 20 Sum: 30
Project 2 — Volume of a Sphere
The volume of a sphere is calculated using:
Volume = (4 / 3) × π × radius³
import math
radius = float(input("Enter radius: "))
volume = (4 / 3) * math.pi * radius ** 3
print("Volume:", round(volume, 2))
Enter radius: 3 Volume: 113.1
Project 3 — Celsius to Fahrenheit
This program converts a temperature from Celsius to Fahrenheit.
Fahrenheit = (Celsius × 9 / 5) + 32
celsius = float(input("Enter Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print("Fahrenheit:", fahrenheit)
Enter Celsius: 25 Fahrenheit: 77.0
Final Practice Challenges
Challenge 1: Create a program that calculates the area of a rectangle using user input.
Show Solution
length = float(input("Length: "))
width = float(input("Width: "))
area = length * width
print("Area:", area)
Length: 10 Width: 5 Area: 50.0
Challenge 2: Write a program that checks whether a number is even or odd.
Show Solution
number = int(input("Enter number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
Enter number: 7 Odd
Challenge 3: Write a program that converts kilometers into meters.
Show Solution
km = float(input("Kilometers: "))
meters = km * 1000
print("Meters:", meters)
Kilometers: 2 Meters: 2000.0
Additional Python Learning Resources
You can learn more and practice Python using these resources.