Python Full Code Sheet with Explanations
1. Basics - Print, Variables, Data Types
print("Hello, world!")
name = "Ali"
age = 16
is_student = True
print(name, age, is_student)
Explanation:
Basic print function jo console pe output deta hai.
Variables banaye gaye: name (string), age (int), is_student (boolean).
2. Input and Type Casting
age = input("Enter your age: ")
age = int(age)
print("You will be", age + 1, "next year!")
Explanation:
input() string leta hai, isliye int() se convert kiya.
Phir usme +1 karke agle saal ki age dikhayi.
3. If-Else Statement
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Explanation:
Agar number 0 se bara ho to positive, 0 ke barabar ho to zero,
Python Full Code Sheet with Explanations
warna negative print karega.
4. While Loop
count = 1
while count <= 5:
print("Count is:", count)
count += 1
Explanation:
Jab tak count 5 se chhota ya barabar hai, loop chalta rahega.
5. For Loop with Range
for i in range(1, 6):
print("i =", i)
Explanation:
range(1, 6) matlab 1 se 5 tak values (6 exclusive).
6. Functions in Python
def greet(name):
print("Hello", name)
greet("Ali")
Explanation:
Function define karne ke liye 'def' use hota hai.
'greet' function ek name leta hai aur hello print karta hai.
7. List in Python
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
Python Full Code Sheet with Explanations
fruits.append("orange")
print(fruits)
Explanation:
List ek ordered collection hai.
append() use hota hai naye item add karne ke liye.
8. Tuple in Python
person = ("Ali", 16, "Student")
print(person[0])
Explanation:
Tuple ek immutable (non-changeable) list jaisa data type hai.
9. Dictionary in Python
student = {"name": "Ali", "age": 16}
print(student["name"])
Explanation:
Dictionary me key-value pairs hote hain.
Key se value access kar sakte ho.
10. File Handling
with open("data.txt", "w") as file:
file.write("Hello World")
with open("data.txt", "r") as file:
content = file.read()
print(content)
Explanation:
Python Full Code Sheet with Explanations
Text file ko write aur read karne ka example hai.
'with open' se file automatically close ho jati hai.
11. Classes and Objects
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
s1 = Student("Ali", 16)
print(s1.name)
Explanation:
Class banai gayi Student naam ki.
__init__ ek constructor hai jo values assign karta hai.
12. Try Except (Error Handling)
try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input!")
Explanation:
try block me code likhte hain jisme error aasakta hai.
except me error handle karte hain.
13. Using Math Library
Python Full Code Sheet with Explanations
import math
print(math.sqrt(16))
print(math.pi)
Explanation:
math library ka use advanced calculations ke liye hota hai.
14. Mini Project - Number Guessing Game
import random
number = random.randint(1, 10)
guess = int(input("Guess a number between 1 and 10: "))
if guess == number:
print("Correct!")
else:
print("Wrong! The number was", number)
Explanation:
Random number generate karke user se guess karwana.
Simple game logic ka example.