CodeEazy

Python Logo

Python Cheat Sheet

Basic Syntax

print("Hello, World!")
name = "CodeEazy"
age = 25
print(f"{name} is {age} years old.")

Data Types

x = 10        # int
y = 3.14     # float
z = "text"   # str
is_valid = True  # bool
my_list = [1, 2, 3]
my_dict = {"a": 1, "b": 2}

Control Structures

if age > 18:
  print("Adult")
else:
  print("Minor")

for i in range(5):
  print(i)

while age > 0:
  print(age)
  age -= 1

Functions

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

print(greet("Sai"))

Classes

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def say_hello(self):
    print(f"Hi, I am {self.name}")

p = Person("Kumar", 30)
p.say_hello()

File Handling

with open("file.txt", "r") as f:
  contents = f.read()
  print(contents)

Common Built-in Functions

len(), type(), str(), int(), float(), input(), range(), list()

Modules

import math
print(math.sqrt(16))

from datetime import datetime
print(datetime.now())