0% found this document useful (0 votes)
12 views3 pages

Python Basics Cheat Sheet

This document is a comprehensive cheat sheet for Python beginners, covering essential topics such as setup, printing, variables, data types, collections, operators, conditionals, loops, functions, classes, input/output, file handling, and error handling. It provides code examples and explanations for each topic to facilitate learning. Additionally, it highlights useful tools like list comprehensions and built-in functions.

Uploaded by

m92156532
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)
12 views3 pages

Python Basics Cheat Sheet

This document is a comprehensive cheat sheet for Python beginners, covering essential topics such as setup, printing, variables, data types, collections, operators, conditionals, loops, functions, classes, input/output, file handling, and error handling. It provides code examples and explanations for each topic to facilitate learning. Additionally, it highlights useful tools like list comprehensions and built-in functions.

Uploaded by

m92156532
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

■ Python Basics Cheat Sheet

A complete guide for Python beginners, covering all essential basics.

1. Python Setup
Install Python from [Link].
Run scripts: python [Link]
Interactive mode: python

2. Printing & Comments


print("Hello, World!")
# Single-line comment
"""
Multi-line
comment
"""

3. Variables & Data Types


x = 10 (int), y = 3.14 (float), name = "Amin" (string), is_on = True (boolean)
Use type(x) to check data type.

4. Numbers
Supports int, float, complex.
Operators: +, -, *, /, //, %, **
Example: 10 // 3 = 3 (floor division).

5. Strings
s = "Python"
Indexing: s[0] = P
Slicing: s[0:3] = Pyt
Useful methods: upper(), lower(), replace(), split()
Formatting: f"My age is {age}"

6. Collections
List: [1,2,3], mutable.
Tuple: (1,2,3), immutable.
Set: {1,2,3}, unique values.
Dictionary: {'key': 'value'}

7. Operators
Arithmetic (+,-,*,/,% etc.), Comparison (==, !=, >), Logical (and, or, not), Membership (in, not in), Identity
(is, is not).

8. Conditionals
if x>0: ... elif x==0: ... else: ...

9. Loops
for i in range(5): ...
while i<5: ...
Break/Continue to control loops.

10. Functions
def greet(name="World"):
return f"Hello, {name}"

11. Classes & Objects


class Person:
def __init__(self, name): [Link]=name
def say(self): print([Link])

12. Input & Output


name = input("Enter name: ")
print("Hello", name)

13. File Handling


with open("[Link]", "w") as f: [Link]("Hello")
with open("[Link]", "r") as f: print([Link]())

14. Error Handling


try: ... except: ... finally: ...
15. Useful Tools
List comprehensions, lambda, len(), max(), sum(), sorted().

You might also like