phyton
phyton
1. Introduction to Python
What is Python?
Features of Python
2. Basic Syntax
Comments
Variables
Print Statements
print("Hello, World!")
3. Data Types
Numbers
Integers (int), floating point numbers (float), and complex numbers (complex).
num1 = 10 # Integer
num2 = 3.14 # Float
num3 = 2 + 3j # Complex
Strings
str1 = "Hello"
str2 = 'World'
str3 = """This is
a multi-line
string"""
Lists
Tuples
Ordered, immutable collection of elements.
Dictionaries
Sets
my_set = {1, 2, 3, 4}
4. Control Flow
If-else Statements
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Elif Statements
x = 20
if x > 30:
print("x is greater than 30")
elif x == 20:
print("x is equal to 20")
else:
print("x is less than 20")
Loops
Defining Functions
def greet(name):
print(f"Hello, {name}")
Return Values
result = add(2, 3)
print(result) # Output: 5
Arguments
Importing Modules
import math
print(math.sqrt(16)) # Output: 4.0
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
p = Person("Alice", 25)
p.greet() # Output: Hello, my name is Alice and I am 25 years old.
Inheritance
Inheritance allows one class to inherit attributes and methods from another class.
class Employee(Person):
def __init__(self, name, age, job):
super().__init__(name, age)
self.job = job
def work(self):
print(f"{self.name} is working as a {self.job}")
Polymorphism
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
d = Dog()
d.speak() # Output: Dog barks
8. Exception Handling
Try-Except Block
try:
x = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
Finally Block
try:
x = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
finally:
print("This will always execute.")
9. File Handling
Opening a File
Reading a File
Installation of Libraries
11. Conclusion
Python is a powerful and versatile language used for web development, data analysis, machine learning, automation, and more. It's beginner-
friendly but also robust enough for professionals. Mastering Python can open doors to many fields in software development.