التخطي إلى المحتوى الرئيسي

Python Cheat Sheet: A Quick Guide for Beginners

Python Cheat Sheet: A Quick Guide for Beginners

Whether you're a Python newbie or a seasoned dev needing a refresher, this cheat sheet has your back. It covers the essentials—variables, control flow, data structures, functions, and more—without any fluff. Let's dive in! 🚀


📌 1. Basics

python
# Comments
print("Hello, Python!") # This prints text
  • Indentation matters (usually 4 spaces)

  • Semicolons are optional—prefer one statement per line


🔢 2. Data Types

python
x = 42 # int
pi = 3.14 # float
name = "Alice" # str
active = True # bool
  • Use type() to check data type

  • Strings are immutable, but lists are not


📋 3. Variables & Operators

python
x += 1 # Same as x = x + 1
  • Math: + - * / // % **

  • Compare: == != > < >= <=

  • Logic: and, or, not


📦 4. Data Structures

Lists

python
nums = [1, 2, 3]
nums.append(4)

Tuples

python
t = (1, 2)

Sets

python
s = {1, 2, 2, 3} # {1, 2, 3}

Dictionaries

python
person = {"name": "Bob", "age": 30}

🔁 5. Control Flow

If-Else

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

Loops

python
for i in range(3):
print(i)
while x > 0:
x -= 1

🧰 6. Functions

python
def greet(name):
return f"Hello, {name}"
  • Use return to return values

  • Use *args, **kwargs for flexible arguments


📂 7. File Handling

python
# Write
with open("file.txt", "w") as f:
f.write("Hello!")
# Read
with open("file.txt", "r") as f:
content = f.read()

🚨 8. Error Handling

python
try:
1 / 0
except ZeroDivisionError:
print("Oops!")
finally:
print("Always runs")

🧪 9. Built-In Functions

  • len(), type(), str(), int(), float()

  • sum(), min(), max(), sorted()


🔍 10. Importing Modules

python
import math
math.sqrt(25) # 5.0
from random import choice
choice(["A", "B", "C"])

📎 Final Thoughts

Python is elegant, versatile, and beginner-friendly. This cheat sheet is just the beginning—explore libraries like pandas, flask, or pygame as you grow.

👉 Bookmark this page for quick reference, and happy coding!

تعليقات

© 2020 To know web

Designed by Open Themes & Nahuatl.mx.