KEMBAR78
Python Study Guide | PDF | Data Type | Boolean Data Type
0% found this document useful (0 votes)
5 views3 pages

Python Study Guide

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)
5 views3 pages

Python Study Guide

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/ 3

1.

Basics: Variables, Data Types, print, input


# Variables
name = "Alice" # string
age = 25 # integer
height = 5.7 # float
is_student = True # boolean

# Printing
print("Name:", name)
print(f"{name} is {age} years old.")

# Input
user_name = input("Enter your name: ")
print("Hello,", user_name)

# Data types
x = 10
print(type(x)) # <class 'int'>

2. Logic: if, else, elif


num = 10
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")

3. Loops: for, while


# for loop
for i in range(5):
print(i) # 0,1,2,3,4

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

4. Collections: lists, dictionaries, tuples, sets


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

# Dictionary
person = {"name": "Bob", "age": 30}
print(person["name"])
person["age"] = 31

# Tuple (immutable)
coordinates = (10, 20)
print(coordinates[0])

# Set (unique values)


unique_nums = {1, 2, 2, 3}
print(unique_nums) # {1, 2, 3}

5. Functions: def, arguments, return values


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

msg = greet("Alice")
print(msg)
6. Modules: importing libraries
import math
print(math.sqrt(16)) # 4.0

from random import randint


print(randint(1, 10))

7. File handling: read/write files


# Write
with open("test.txt", "w") as f:
f.write("Hello world")

# Read
with open("test.txt", "r") as f:
content = f.read()
print(content)

8. OOP (Object-Oriented Programming): classes & objects


class Person:
def __init__(self, name, age): # constructor
self.name = name
self.age = age

def greet(self):
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")

p1 = Person("Alice", 25)
p1.greet()

9. Popular libraries: pandas


import pandas as pd
data = {"Name": ["Alice", "Bob"], "Age": [25, 30]}
df = pd.DataFrame(data)
print(df)

matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 4, 6]
plt.plot(x, y)
plt.show()

requests
import requests
response = requests.get("https://api.github.com")
print(response.status_code)
print(response.json())

flask
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)

fastapi
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

You might also like