Introduction to Python
Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.
Key Features
- Easy to learn and use
- Interpreted language
- Extensive standard library
- Cross-platform compatibility
- Supports multiple programming paradigms
Installation
sudo apt install python3
Or download from python.org.
Data Types
- int
- float
- str
- bool
- list
- tuple
- dict
- set
Operators
- Arithmetic: +, -, *, /, %, **, //
- Assignment: =, +=, -=, etc.
- Comparison: ==, !=, <, >, <=, >=
- Logical: and, or, not
Conditions
if a > b:
print("a is greater than b")
elif a == b:
print("a is equal to b")
else:
print("a is less than b")
Loops
for i in range(5):
print(i)
while a < 10:
a += 1
Functions
def greet(name):
return f"Hello, {name}!"
File Handling
with open("test.txt", "r") as file:
for line in file:
print(line)
Class & Object
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, my name is {self.name}")
p = Person("Sai")
p.greet()
Other Features
- Exception handling with try/except
- Modules and packages
- Virtual environments