Python is a high-level, interpreted programming language known for its simplicity, readability, and power. It’s used in AI, web apps, data science, automation, machine learning, and more.
Why Learn Python?
- Simple syntax → easy for beginners.
- Cross-platform → works everywhere.
- Massive community → huge libraries & frameworks.
- Versatile → from websites to AI models.
Hello World
print("Hello, Python!")
Variables
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Data Types
- String →
"Hello"
- Integer →
42
- Float →
3.14
- Boolean →
True
/False
- List →
[1, 2, 3]
- Tuple →
(1, 2, 3)
- Dictionary →
{"name": "Alice", "age": 25}
Conditionals
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
Loops
# For loop
for i in range(1, 6):
print(i)
# While loop
x = 0
while x < 3:
print(x)
x += 1
Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Lists
fruits = ["Apple", "Banana", "Cherry"]
for fruit in fruits:
print(fruit)
Dictionaries
person = {"name": "Alice", "age": 25}
print(person["name"])
print(person.get("age"))
Classes & Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name}."
p1 = Person("Alice", 25)
print(p1.greet())
File Handling
# Writing to a file
with open("data.txt", "w") as f:
f.write("Hello, File!")
# Reading from a file
with open("data.txt", "r") as f:
print(f.read())
Mini Project: Simple Calculator
def add(a, b): return a + b
def sub(a, b): return a - b
def mul(a, b): return a * b
def div(a, b): return a / b
print(add(5, 3))
print(sub(10, 4))
print(mul(2, 6))
print(div(8, 2))
Popular Libraries
- Django / Flask → Web development
- Pandas → Data analysis
- Numpy → Math & arrays
- TensorFlow / PyTorch → Machine learning
- Requests → HTTP requests
Best Practices
- Use
virtualenv
for isolated environments. - Follow PEP 8 for clean code style.
- Write small, reusable functions.
- Test your code regularly.
Next Steps
- Build small projects (calculator, to-do app, scraper).
- Learn OOP deeply (classes, inheritance, polymorphism).
- Explore libraries for your field (Flask, Pandas, TensorFlow).
- Contribute to open-source Python projects.
Conclusion
Python is a must-learn language for modern developers. Its simplicity, power, and wide usage make it the perfect tool for beginners and professionals alike. From AI to web apps, Python can do it all 🚀🔥.
No comments:
Post a Comment