[go: up one dir, main page]

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

Python Cheatsheat

This Python cheat-sheet covers essential topics including basic syntax, data types, control flow, functions, lists, dictionaries, file handling, error handling, useful built-in functions, and modules. It provides examples for each topic to illustrate their usage. This resource is designed for quick reference and learning of Python programming concepts.

Uploaded by

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

Python Cheatsheat

This Python cheat-sheet covers essential topics including basic syntax, data types, control flow, functions, lists, dictionaries, file handling, error handling, useful built-in functions, and modules. It provides examples for each topic to illustrate their usage. This resource is designed for quick reference and learning of Python programming concepts.

Uploaded by

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

Python Cheat-sheet

1. Basic Syntax

 Print Statement:

print("Hello, World!")

 Comments:
 # This is a single-line comment
 """This is a
multi-line comment"""

 Variables:
 x = 10 # Integer
 y = 3.14 # Float
name = "Alice" # String

2. Data Types

 Common Data Types:

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

 Examples:
 is_happy = True # Boolean
 numbers = [1, 2, 3] # List
 point = (4, 5) # Tuple
 info = {"name": "Bob", "age": 25} # Dictionary
unique_numbers = {1, 2, 3} # Set

3. Control Flow

 If Statements:
 age = 18
 if age >= 18:
 print("Adult")
 else:
print("Minor")

 Loops:
 # For Loop
 for i in range(5):
 print(i)

 # While Loop
 count = 0
 while count < 5:
 print(count)
count += 1

4. Functions

 Defining and Calling Functions:


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

print(greet("Alice"))

5. Lists

 List Operations:
 fruits = ["apple", "banana", "cherry"]
 fruits.append("orange") # Add an item
 fruits.remove("banana") # Remove an item
print(fruits[0]) # Access by index

6. Dictionaries

 Dictionary Operations:
 person = {"name": "John", "age": 30}
 print(person["name"]) # Access value
 person["age"] = 31 # Update value
person["city"] = "NY" # Add key-value pair

7. File Handling

 Reading and Writing Files:


 # Writing to a file
 with open("example.txt", "w") as file:
 file.write("Hello, World!")

 # Reading from a file
 with open("example.txt", "r") as file:
 content = file.read()
print(content)

8. Error Handling

 Try-Except Blocks:
 try:
 x = 10 / 0
 except ZeroDivisionError:
print("Cannot divide by zero!")

9. Useful Built-in Functions

 Common Functions:
 len(sequence) # Get length
 type(variable) # Get type
 str(), int(), float() # Convert types
sorted(list) # Sort list

10. Modules

 Importing Modules:
 import math
 print(math.sqrt(16))

 from random import randint
print(randint(1, 10))

You might also like