1. What is Python?
Python is:
2. Installation of Python on Windows and Linux
To start using Python:
3. Verify installation
Verify Installation of python by typing 'python --version' in the terminal or command prompt.
It should display the installed version of Python.
4. Writing Your First Python Program
You can run Python code in:
Hello, World! Example
print("Hello, World..!")Run the code, and output is: Hello, World..!
5. Python Basics
Variables and Data Types
Variables store data. You don’t need to declare their type.
# Variables name = "Alice" # String age = 25 # Integer height = 5.7 # Float is_student = True # Boolean print(name, age, height, is_student)
Input and Output
• Input: Get data from the user.
• Output: Display data to the user.
name = input("Enter your name: ") print(f"Hello, {name}!")
Control Flow
# If-Else Statementage = int(input("Enter your age: ")) if age >= 18: print("You are an adult.") else: print("You are a minor.")
Loops
# For loopfor i in range(5): print(i)# While loop
count = 0 while count < 5: print(count) count += 1
6. Functions in python
Functions help organize reusable code.
def greet(name): return f"Hello, {name}!" print(greet("Alice"))
7. Data Structures in python
Python provides built-in data structures for organizing data.
Lists: Ordered, mutable collections.fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits)Tuples: Ordered, immutable collections.
coordinates = (10, 20) print(coordinates) • Dictionaries: Key-value pairs. person = {"name": "Alice", "age": 25} print(person["name"])• Sets: Unordered collections of unique elements.
unique_numbers = {1, 2, 3, 2} print(unique_numbers) # Output: {1, 2, 3}
8. Working with Libraries in python
Python’s strength lies in its rich ecosystem of libraries.
• Install Libraries: Use pip (Python's package manager) to install libraries.
pip install numpy• Example: Using the math Library
import math print(math.sqrt(16)) # Output: 4.0
9. Error Handling
Python uses try, except, and finally for handling exceptions.
try: result = 10 / 0 except ZeroDivisionError: print("You can't divide by zero!") finally: print("Execution finished.")
10. File Handling in python
Python makes it easy to work with files.
# Writing to a filewith open("example.txt", "w") as file: file.write("Hello, World!")# Reading from a file
with open("example.txt", "r") as file: print(file.read())
11. Object-Oriented Programming
Python supports object-oriented programming (OOP) using classes and objects.
class Car: def __init__(self, make, model): self.make = make self.model = model def display_info(self): return f"{self.make} {self.model}"
my_car = Car("Toyota", "Corolla") print(my_car.display_info()) # Output: Toyota Corolla
OOP allows you to create reusable code and model real-world entities.
Example: Creating a Person class
class Person: def __init__(self, name, age): self.name = name self.age = age def introduce(self): return f"My name is {self.name}, and I am {self.age} years old." alice = Person("Alice", 25) print(alice.introduce())
12. Python Ecosystem
Python’s ecosystem is vast and diverse, with libraries and frameworks catering to various domains. Here are some popular libraries:
• Web Development: Flask, Django
• Data Science: Pandas, NumPy, Matplotlib
• Machine Learning: Scikit-learn, TensorFlow, PyTorch
• Automation: Selenium, PyAutoGUI