📱
Python CheatSheet 1.
Basic Syntax - Print Statement: print("Hello, World!")
- Comments: # This is a comment
2.
Data Types - Integer: x = 10
- Float: y = 10.5
- String: name = "Alice"
- List: fruits = ["apple", "banana", "cherry"]
- Tuple: coordinates = (10, 20)
- Dictionary: person = {"name": "Alice", "age": 25}
3.
Control Structures - If Statement:
if x > 10:
print("x is greater than 10")
- For Loop:
for fruit in fruits:
print(fruit)
- While Loop:
while x < 5:
x += 1
4.
Functions - Define Function:
def greet(name):
return f"Hello, {name}!"
- Lambda Function: add = lambda a, b: a + b
5.
Exception Handling - Try-Except Block:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
6.
File I/O - Read File:
with open('file.txt', 'r') as file:
content = file.read()
- Write File:
with open('file.txt', 'w') as file:
file.write("Hello, World!")
7.
List Comprehensions - Basic Example: squared = [x**2 for x in range(10)]
- Conditional Comprehension: even_squares = [x**2 for x in range(10) if x % 2 == 0]
8.
Modules and Packages - Import Module: import math
- Import Specific Function: from math import sqrt
9.
Common Libraries - NumPy: import numpy as np
- Pandas: import pandas as pd
- Matplotlib: import matplotlib.pyplot as plt
10.
Object-Oriented Programming - Define Class:
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return "Woof!"
11.
Virtual Environments - Create Environment: python -m venv myenv
- Activate Environment:
- Windows: myenv\Scripts\activate
- macOS/Linux: source myenv/bin/activate
12.
Common Commands - Run Script: python script.py
- Install Package: pip install package_name
- List Installed Packages: pip list
This Python checklist serves as a quick reference for essential syntax, functions, and best practices to enhance your coding efficiency!
#Python #Programming
┏━━━━━━━━┓
〓
@SecBookSs┗━━━━━━━━┛