0% found this document useful (0 votes)
4 views2 pages

Python Basics Explained

Uploaded by

sfynhm799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Basics Explained

Uploaded by

sfynhm799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Basics Explained

1. Variables & Data Types:


- Variables are used to store data. You don’t need to declare the type explicitly, Python detects it
automatically.
Example: x = 5 (int), y = 3.14 (float), name = "John" (string), flag = True (boolean).

2. Operators:
- Arithmetic: +, -, *, /, % (modulus), ** (power), // (floor division).
- Comparison: == (equal), != (not equal), >, <, >=, <= (used in conditions).
- Logical: and, or, not (used for combining conditions).

3. Control Flow:
- if, elif, else: Used for decision making.
Example:
if x > 10:
print("Big")
elif x == 10:
print("Equal")
else:
print("Small")

- Loops:
for: used to repeat over a sequence (list, range).
Example: for i in range(5): print(i)
while: runs while a condition is True.

- break stops the loop, continue skips one iteration.

4. Functions:
- Functions are blocks of reusable code.
Example:
def greet(name):
return "Hello " + name

- Functions can take parameters and return values.

5. Collections:
- List: ordered, mutable. Example: numbers = [1, 2, 3]
- Tuple: ordered, immutable. Example: point = (10, 20)
- Set: unordered, unique values. Example: fruits = {"apple", "banana"}
- Dict: key-value pairs. Example: person = {"name": "Ali", "age": 25}

6. Strings:
- Strings are text inside quotes. Example: s = "Hello"
- Common methods: s.lower(), s.upper(), s.split(), " ".join(list)
- Strings can be sliced: s[0:3] gives "Hel".

7. Input & Output:


- input() is used to take input from the user. Example: name = input("Enter your name: ")
- print() is used to display output. Example: print("Hello", name)

8. Modules:
- Python has built-in and external modules (libraries).
- Example: import math → math.sqrt(16)
- from random import randint → randint(1,10)

9. File Handling:
- Used to read/write files.
- Example:
f = open("file.txt", "w")
f.write("Hello World")
f.close()

f = open("file.txt", "r")
print(f.read())
f.close()

10. Exceptions:
- Errors can happen, exceptions handle them safely.
Example:
try:
x = 10 / 0
except Exception as e:
print("Error:", e)

---
This document gives beginners a simple but clear explanation of Python basics with examples.

You might also like