You’re the Expert!

python_quick_reference

Cheatsheets

Perform Arithmetic Operation with 2 Numbers and show result on the screen
print("Addition of 58 and 26:", 58+26)
print("Subtract 26 from 58:", 58-26)
print("Multiply 58 and 36:", 58*26)
print("Divide 58 with 16:", 58/16)
print("Divide 58 with 16 and show integer value:", 58//16)
print("Divide 58 with 16 and show reminder value:", 58%16)
print("5 to the power of 3:", 5**3)
copy to clipboard
execute code
Code console output
Define a code block with print statement and call it
def welcome(name):
  print(f"Hello, {name} Welcome to Pynfinity!")

welcome("World")
copy to clipboard
execute code
Code console output
Variables and Data Types in Python
# Integer
age = 25
print("Age:", age)

# String
name = "Santosh"
print("Name:", name)

# Float
height = 5.9
print("Height:", height)

# Boolean
is_student = True
print("Is Student:", is_student)
copy to clipboard
execute code
Code console output
Working with Lists
fruits = ["apple", "banana", "cherry"]
print("List of fruits:", fruits)

# Accessing elements
print("First fruit:", fruits[0])

# Adding items
fruits.append("orange")
print("Updated list:", fruits)

# Removing items
fruits.remove("banana")
print("List after removing 'banana':", fruits)
copy to clipboard
execute code
Code console output
Conditionals and if-else statements
x = 10
if x > 5:
  print("x is greater than 5")
elif x == 5:
  print("x is equal to 5")
else:
  print("x is less than 5")
copy to clipboard
execute code
Code console output
For Loop Example
# Loop through a list
for fruit in fruits:
  print(fruit)
copy to clipboard
execute code
Code console output
While Loop Example
count = 0
while count < 5:
  print("Count is:", count)
  count += 1
copy to clipboard
execute code
Code console output
Functions and Return Values
def add_numbers(a, b):
  return a + b

result = add_numbers(10, 20)
print("Result of addition:", result)
copy to clipboard
execute code
Code console output

Dictionaries in Python
person = {
  "name": "Santosh",
  "age": 30,
  "city": "India"
}
print("Person Dictionary:", person)

# Accessing a value
print("Person's name:", person["name"])

# Adding a new key-value pair
person["job"] = "Engineer"
print("Updated Person Dictionary:", person)
copy to clipboard
execute code
Code console output
String Manipulation
text = "Hello, Python!"

# String length
print("Length of string:", len(text))

# Convert to uppercase
print("Uppercase:", text.upper())

# Replace a substring
new_text = text.replace("Python", "World")
print("Replaced text:", new_text)
copy to clipboard
execute code
Code console output
List Comprehensions
numbers = [1, 2, 3, 4, 5]

# Using list comprehension to square each number
squares = [num**2 for num in numbers]
print("Squared numbers:", squares)
copy to clipboard
execute code
Code console output
Error Handling with Try-Except
try:
  x = 10 / 0
except ZeroDivisionError:
  print("Cannot divide by zero!")
else:
  print("Division successful!")
copy to clipboard
execute code
Code console output
Lambda Functions
# A simple lambda function to add two numbers
add = lambda a, b: a + b
result = add(5, 7)
print("Result of addition:", result)
copy to clipboard
execute code
Code console output
Working with Files
# Writing to a file
with open("sample.txt", "w") as file:
  file.write("Hello, this is written with Python.")

# Reading from a file
with open("sample.txt", "r") as file:
  content = file.read()
  print("File content:", content)
copy to clipboard
execute code
Code console output

Object-Oriented Programming (OOP)
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def greet(self):
    print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating an object of the Person class
person1 = Person("Santosh", 25)
person1.greet()
copy to clipboard
execute code
Code console output
Generators in Python
# A simple generator function to yield numbers
def count_up_to(max):
  count = 1
  while count <= max:
    yield count
    count += 1

# Using the generator
counter = count_up_to(5)
for num in counter:
  print(num)
copy to clipboard
execute code
Code console output
Decorators in Python
def decorator(func):
  def wrapper():
    print("Before function call")
    func()
    print("After function call")
  return wrapper

@decorator
def say_hello():
  print("Hello!")

say_hello()
copy to clipboard
execute code
Code console output
Handling JSON Data
import json

# Convert Python dictionary to JSON
person = {"name": "Alice", "age": 30}
person_json = json.dumps(person)
print("JSON representation:", person_json)

# Convert JSON back to Python dictionary
person_dict = json.loads(person_json)
print("Python dictionary:", person_dict)
copy to clipboard
execute code
Code console output