Basics Of python part 1🧠

 

🛠️ Setting Up Python

  1. Install Python:

  2. Check Installation:

    bash
    python --version
  3. Run Python:

    • Use an IDE (VS Code, PyCharm, etc.)

    • Or run code via terminal/command line:

      bash
      python

🧠 Basic Concepts

1. Hello World

print("Hello, World!")

2. Variables

name = "Alice" age = 25 pi = 3.14

3. Data Types

  • int, float, str, bool, list, tuple, dict, set, None

a = 10 # int b = 3.14 # float c = "Python" # str d = True # bool

4. Type Checking


print(type(a)) # <class 'int'>

🔁 Control Structures

1. If/Else


x = 5 if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")

2. Loops

For Loop:

for i in range(5): print(i)

While Loop:

count = 0 while count < 5: print(count) count += 1

📦 Data Structures

1. Lists

fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits[1]) # banana

2. Tuples

point = (10, 20)

3. Dictionaries

person = {"name": "Alice", "age": 25} print(person["name"])

4. Sets

colors = {"red", "green", "blue"} colors.add("yellow")

🧰 Functions

def greet(name): return f"Hello, {name}" print(greet("Bob"))

📄 File Handling

# Write to file with open("example.txt", "w") as file: file.write("Hello, file!") # Read from file with open("example.txt", "r") as file: content = file.read() print(content)

🐞 Error Handling

try: x = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") finally: print("Done")

🔄 Object-Oriented Programming (OOP)

class Person: def __init__(self, name): self.name = name def greet(self): print(f"Hi, I'm {self.name}") p = Person("Alice") p.greet()

🧪 Modules & Libraries

import math print(math.sqrt(16))

Use third-party packages:

pip install numpy

Post a Comment

0 Comments