1: Introduction to Python
• Python is a popular, high-level programming language known for its simplicity and
readability.
• It is versatile, used in web development, data analysis, machine learning, automation, and
more.
• Python emphasizes easy-to-understand syntax, making it an ideal choice for beginners.
• It supports multiple programming paradigms like procedural, object-oriented, and functional
programming.
Code Example:
python
Copy
print("Hello, Python!")
2: Installing and Setting Up Python
• Download the latest version of Python from the official website: [Link].
• Install an Integrated Development Environment (IDE) such as PyCharm or Visual Studio Code.
• Python comes with an interactive shell called IDLE, which can also be used to run code.
• Make sure Python is added to your system's PATH during installation for easy access.
Code Example:
bash
Copy
# Check if Python is installed:
python –version
3: Python Syntax and Code Structure
• Python uses indentation (spaces or tabs) to define blocks of code, instead of curly braces {}.
• It is important to maintain consistent indentation to avoid errors.
• Python does not require semicolons to end statements, making code cleaner and easier to
read.
• Statements can span multiple lines, making it more flexible for writing readable code.
Code Example:
python
Copy
def greet(name):
print(f"Hello, {name}!")
greet("Python")
4: Variables and Data Types
• Variables store data, and Python automatically infers the data type based on the value
assigned.
• Common data types include integers (int), floating-point numbers (float), strings (str), and
booleans (bool).
• Lists, tuples, and dictionaries are common collection types in Python.
Code Example:
python
Copy
x=5 # int
name = "John" # str
is_active = True # bool
5: Operators in Python (Arithmetic, Logical, Comparison, etc.)
• Arithmetic operators: +, -, *, /, //, %, **.
• Comparison operators: ==, !=, <, >, <=, >=.
• Logical operators: and, or, not.
Code Example:
python
Copy
a = 10
b = 20
# Arithmetic
sum = a + b
# Comparison
result = a < b
# Logical
check = a > 5 and b < 30
6: Type Conversion and Casting
• Type conversion allows you to convert one data type to another.
• You can use built-in functions like int(), float(), and str() to perform type casting.
• Python also supports implicit type conversion.
Code Example:
python
Copy
x = "10" # string
y = int(x) # casting string to integer
z = 5.7 # float
w = int(z) # casting float to integer
7: Control Flow Statements (if-else)
• if-else statements allow the program to make decisions based on conditions.
• The if block executes if the condition is true; otherwise, the else block runs.
• You can also use elif for multiple conditions.
Code Example:
python
Copy
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
Slide 8: Loops (for, while)
• for loop is used for iterating over a sequence like a list or range.
• while loop repeats a block of code as long as the condition is true.
Code Example:
python
Copy
# For loop example
for i in range(5):
print(i)
# While loop example
x=0
while x < 5:
print(x)
x += 1
9: Break, Continue, and Pass Statements
• break: Exits the loop when a condition is met.
• continue: Skips the current iteration and moves to the next one.
• pass: A placeholder that does nothing, useful for stubbing out code.
Code Example:
python
Copy
# break example
for i in range(10):
if i == 5:
break
print(i)
# continue example
for i in range(5):
if i == 3:
continue
print(i)
# pass example
def empty_function():
pass
10: Functions and Function Arguments
• Functions are reusable blocks of code that perform a specific task.
• You can pass data to functions via arguments and return values using return.
• Python supports default, keyword, and variable-length arguments.
Code Example:
python
Copy
def greet(name="User"):
print(f"Hello, {name}!")
greet("Alice")
greet() # Will use the default argument