Python Notes Part1-1
Python Notes Part1-1
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.
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.
1. Open VS Code.
2. Go to Extensions (Ctrl + Shift + X).
3. Search for Python.
4. Click Install on the extension by Microsoft.
1. Open VS Code.
2. Press Ctrl + Shift + P → Search "Python: Select Interpreter".
3. Select the installed Python version.
print("Hello, Python!")
Enterprise
Use Cases Web, AI, Automation System Programming, Games
Applications
Explanation:
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.
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.
x=5 # Integer
name = "John" # String
price = 99.99 # Float
is_valid = True # Boolean
Rules :
name = "Vasu"
str Text (must be in quotes)
name = ‘Vasu’
y = "Hello"
print(type(y)) # Output: <class 'str'>
Function Converts To
int(x) Integer
float(x) Float
str(x) String
bool(x) Boolean
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'>
5 Comments in Python
# This is a comment
'''
This is a multi-line comment.
It spans multiple lines.
''
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
num1 = 10
num2 = 20
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."
== Equal to a == b → False
and Returns True if both conditions are True (a > 0 and b > 0)
age = 20
has_license = True
3 Loops in Python
Loops help in executing a block of code multiple times. Python has two main types of loops:
for loop Used for iterating over a sequence (list, tuple, string, etc.)
4 for Loop
The for loop is commonly used to iterate over a sequence (list, tuple, string, etc.).
0
1
2
3
4
1
3
5
7
9
5 while Loop
while condition:
# Code block to be executed
count = 1
while count <= 5:
print(count)
count += 1 # Increment count
Explaination: Prints numbers 1 to 5, then stops.
while True:
print("This will run forever!") # Press Ctrl+C to stop
Note: Make sure to use a stopping condition to avoid infinite loops!
Statement Description
continue Skips the rest of the code and moves to the next iteration
Example of break
0
1
2
3
4
-> The loop stops when num reaches 5.
Example of continue
0
1
3
4
� The loop skips 2 but continues with the next numbers.
7 Nested Loops
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.