PYTHON TRAINING NOTES DREAM A DREAM
Introduction to Python
1 What is Python?
Python is a high-level, interpreted, and dynamically typed programming language designed for
simplicity and readability. It is widely used for web development, data science, machine learning,
automation, and more.
2 Why Use Python?
✅ Easy to Learn & Read – Simple English-like syntax
✅ Interpreted Language – Executes code line by line
✅ Dynamically Typed – No need to declare variable types explicitly
✅ Large Standard Library – Supports file handling, networking, and automation
✅ Cross-Platform – Runs on Windows, macOS, and Linux
✅ Open Source – Free to use with strong community support
3 How to Install Python?
Step 1: Download Python
1. Go to the official Python website: https://www.python.org/downloads/
2. Click on the latest Python version (recommended for your OS).
3. Download the installer based on your operating system
Step 2: Install Python (Windows)
1. Open the downloaded .exe file.
2. ✅ Check the box "Add Python to PATH" (Important!)
3. Click Install Now and wait for the installation to complete.
4. Click Finish after installation.
PREPARED BY HARISH YADAV Page 1
PYTHON TRAINING NOTES DREAM A DREAM
Step 3: Verify Installation
Open Command Prompt (Windows) / Terminal (Mac/Linux)
Type: python –version
If installed correctly, it will show the Python version
4 How to Install VS Code for Python?
Step 1: Download VS Code
1. Visit https://code.visualstudio.com/
2. Download VS Code for your OS (Windows, Mac, or Linux).
3. Run the installer and follow the instructions.
Step 2: Install Python Extension in VS Code
1. Open VS Code.
2. Go to Extensions (Ctrl + Shift + X).
3. Search for Python.
4. Click Install on the extension by Microsoft.
Step 3: Configure Python Interpreter
1. Open VS Code.
2. Press Ctrl + Shift + P → Search "Python: Select Interpreter".
3. Select the installed Python version.
Step 4: Run a Python Program in VS Code
1. Open VS Code → Click File → New File.
2. Save the file as hello.py.
3. Write the following code:
print("Hello, Python!")
4. Click Run (▶ button) or use the terminal command (python hello.py)
PREPARED BY HARISH YADAV Page 2
PYTHON TRAINING NOTES DREAM A DREAM
5 What are the uses of Python?
Python is used in various fields, including:
Web Development – Django, Flask
Data Science & Analysis – Pandas, NumPy
Machine Learning & AI – TensorFlow, PyTorch
Automation & Scripting – Web scraping, task automation
Cybersecurity – Ethical hacking, penetration testing
Game Development – Pygame
6 Python v/s Other Programming Languages
Feature Python Java C++
Syntax Simple, readable Verbose Complex, many symbols
Execution Interpreted Compiled (JVM) Compiled (Machine Code)
Speed Slower (interpreted) Faster than Python Fastest (close to hardware)
Higher (dynamic Lower (manual memory
Memory Moderate
typing) management)
Enterprise
Use Cases Web, AI, Automation System Programming, Games
Applications
7 First Python Program
# This program prints a message
print("Hello, World!")
Explanation:
print() is a built-in function that outputs text to the screen.
The # symbol is used for comments (ignored by Python).
Running the program will display: Hello, World!
PREPARED BY HARISH YADAV Page 3
PYTHON TRAINING NOTES DREAM A DREAM
8 Python Syntax
Python syntax is simple and easy to read. Here are some key rules:
Indentation matters – Unlike other languages that use {} or ;, Python relies on indentation
(spaces/tabs) to structure code.
Case-sensitive – name and Name are treated as different variables.
Statements end with a new line – No need for semicolons (;) like C or Java, but you can use them
if needed.
Dynamic Typing – No need to declare variable types explicitly.
Example: Proper Indentation in Python
# Correct indentation
if 10 > 5:
print("10 is greater than 5") # Indented block
# Incorrect indentation (will cause an error)
if 10 > 5:
print("10 is greater than 5") # ❌ This will give an
IndentationError
Variables in Python
1 What is a Variable?
A variable is a container for storing data. Python automatically determines the variable type based on the
assigned value.
2 Declaring Variables & Rules for Variable Naming
x=5 # Integer
name = "John" # String
price = 99.99 # Float
is_valid = True # Boolean
PREPARED BY HARISH YADAV Page 4
PYTHON TRAINING NOTES DREAM A DREAM
Rules :
✔ Can contain letters, digits, and underscores (_)
✔ Cannot start with a digit (1name ❌)
✔ Case-sensitive (name and Name are different)
✔ Cannot use Python keywords (if, while, class, etc.)
3 Data Types in Python
Python supports different data types:
Data Type Example Description
int x = 10 Whole numbers
float y = 5.5 Decimal numbers
name = "Vasu"
str Text (must be in quotes)
name = ‘Vasu’
bool is_valid = True Boolean values (True/False)
Checking Data Type
To check a variable’s type, use the type() function:
x = 10
print(type(x)) # Output: <class 'int'>
y = "Hello"
print(type(y)) # Output: <class 'str'>
PREPARED BY HARISH YADAV Page 5
PYTHON TRAINING NOTES DREAM A DREAM
4 Type Casting (Converting Data Types)
Python allows converting data types using built-in functions:
Function Converts To
int(x) Integer
float(x) Float
str(x) String
bool(x) Boolean
Example of Type Casting :
x = "10"
y = int(x) # Converts string "10" to integer 10
print(y, type(y)) # Output: 10 <class 'int'>
a=5
b = float(a) # Converts integer 5 to float 5.0
print(b, type(b)) # Output: 5.0 <class 'float'>
c = bool(0) # Converts 0 to False
print(c) # Output: False
5 Comments in Python
Comments are ignored by Python and used to explain code.
Single-line Comment (#)
# This is a comment
PREPARED BY HARISH YADAV Page 6
PYTHON TRAINING NOTES DREAM A DREAM
x = 5 # This is also a comment
Multi-line Comment (''' or """)
'''
This is a multi-line comment.
It spans multiple lines.
''
Conditional Statements & Loops
1 Conditional Statements (if-elif-else)
Conditional statements allow a program to make decisions based on certain conditions.
Syntax of if-elif-else Statement
if condition:
# Code block executes if condition is True
elif another_condition:
# Code block executes if first condition is False but this is True
else:
# Code block executes if all conditions are False
Example of if-elif-else (Finding the Greatest of Two Numbers)
num1 = 10
num2 = 20
if num1 > num2:
print(num1, "is greater than", num2)
elif num1 < num2:
print(num2, "is greater than", num1)
else:
print("Both numbers are equal")
PREPARED BY HARISH YADAV Page 7
PYTHON TRAINING NOTES DREAM A DREAM
Explanation:
If num1 is greater than num2, it prints that num1 is greater than num2.
If num1 is less than num2, it prints that num2 is greater than num1.
If both numbers are equal, it prints "Both numbers are equal."
Comparison Operators in Conditions
Operator Description Example (a = 5, b = 10)
== Equal to a == b → False
!= Not equal to a != b → True
> Greater than a > b → False
< Less than a < b → True
>= Greater than or equal to a >= b → False
<= Less than or equal to a <= b → True
2 Logical Operators in Conditions
Logical operators allow us to combine multiple conditions.
Operator Description Example
and Returns True if both conditions are True (a > 0 and b > 0)
or Returns True if at least one condition is True (a > 0 or b < 0)
PREPARED BY HARISH YADAV Page 8
PYTHON TRAINING NOTES DREAM A DREAM
Operator Description Example
not Reverses the condition not (a > 0)
Example of Logical Operators
age = 20
has_license = True
if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")
Explaination: If age is 18 or above AND the person has a license, they can drive.
3 Loops in Python
Loops help in executing a block of code multiple times. Python has two main types of loops:
Loop Type Description
for loop Used for iterating over a sequence (list, tuple, string, etc.)
while loop Executes as long as a condition is True
4 for Loop
The for loop is commonly used to iterate over a sequence (list, tuple, string, etc.).
PREPARED BY HARISH YADAV Page 9
PYTHON TRAINING NOTES DREAM A DREAM
Syntax of for Loop
for variable in sequence:
# Code block to be executed
Example: Looping through a List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Explaination: Prints each fruit in the list one by one.
Using range() in for Loops
The range() function generates a sequence of numbers.
for i in range(5): # Loops from 0 to 4
print(i)
Output:
0
1
2
3
4
Loop with Step Value
for i in range(1, 10, 2): # Start from 1, go up to 10 (excluding 10), step by 2
print(i)
Output:
1
3
5
7
9
PREPARED BY HARISH YADAV Page 10
PYTHON TRAINING NOTES DREAM A DREAM
Looping Through a String
for char in "Python":
print(char)
Explaination: Prints each character of "Python" separately.
5 while Loop
The while loop executes as long as a condition remains True.
Syntax of while Loop
while condition:
# Code block to be executed
Example: Basic while Loop
count = 1
while count <= 5:
print(count)
count += 1 # Increment count
Explaination: Prints numbers 1 to 5, then stops.
Infinite Loop (Avoid!)
while True:
print("This will run forever!") # Press Ctrl+C to stop
Note: Make sure to use a stopping condition to avoid infinite loops!
PREPARED BY HARISH YADAV Page 11
PYTHON TRAINING NOTES DREAM A DREAM
6 break and continue Statements in Loops
Statement Description
break Exits the loop immediately
continue Skips the rest of the code and moves to the next iteration
Example of break
for num in range(10):
if num == 5:
break # Stops loop when num is 5
print(num)
Output:
0
1
2
3
4
-> The loop stops when num reaches 5.
Example of continue
for num in range(5):
if num == 2:
continue # Skips the rest of the code for num = 2
print(num)
Output:
0
1
3
4
� The loop skips 2 but continues with the next numbers.
PREPARED BY HARISH YADAV Page 12
PYTHON TRAINING NOTES DREAM A DREAM
7 Nested Loops
A loop inside another loop is called a nested loop.
Example of Nested for Loops
for i in range(3): # Outer loop
for j in range(2): # Inner loop
print("i =", i, ", j =", j)
Output:
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
-> The outer loop runs 3 times, and the inner loop runs 2 times for each outer loop cycle.
PREPARED BY HARISH YADAV Page 13