0% found this document useful (0 votes)
2 views4 pages

Python

This document is a Python cheat sheet covering various programming concepts including basics, data types, conditionals, loops, functions, strings, lists, tuples, sets, dictionaries, comprehensions, file handling, modules, packages, and exceptions. Each section provides code snippets and brief explanations for quick reference. It is aimed at Python enthusiasts and includes a note about additional resources available on Telegram.

Uploaded by

rkrahul13206
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)
2 views4 pages

Python

This document is a Python cheat sheet covering various programming concepts including basics, data types, conditionals, loops, functions, strings, lists, tuples, sets, dictionaries, comprehensions, file handling, modules, packages, and exceptions. Each section provides code snippets and brief explanations for quick reference. It is aimed at Python enthusiasts and includes a note about additional resources available on Telegram.

Uploaded by

rkrahul13206
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

python <sWIPE=

PYTHON CHEAT SHEET

BASICS
print("Hello, World!") # Print output
X = 10 # Variable assignment
type(x) # Get type of variable
input("Enter value: ") # Take user input

DATA TYPES
my_list = [1, 2, 3] # List
my_tuple = (1, 2, 3) # Tuple
my_set = {1, 2, 3} # Set
my_dict = {"key" : "value"} # Dictionary

cONDITIONALS
if x > 5:
print("Greater than 5") # If condition
elif x == 5:
print("Equals 5") # Else-if
else:
print(" Less than 5") # Else

LOOPS
for i in range(5):
print (i) # For loop
while x > 0:
print(x)
X -= 1 # While loop

AKASH" PYTHON &TECH ENTHUSIAST ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] CHECK THE LINK IN BIO
python <sWIPES

FUNCTIONS
def greet(name):
return "Hello " + name # Function

greet("Akash") # Call function

STRINGS
s = "Python"
[Link]() # To lowercase
[Link]) # To uppercase
[Link](" ") # Split string
[Link]("Py", "My") # Replace text

LISTS
my_list.append(4) # Add element
my_list.remove(2) # Remove element
my_list [o] # Access index
my_list[: :-1] # Reverse list

TUPLES
t = (1, 2, 3)
t[o] # Access element
len(t) # Length

[Link] &TECH ENTHUSIAST ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] CHECK THE LINK IN BIO
python <sWIPE=

SETS
s = {1, 2, 3}
[Link](4) # Add element
[Link] (2) # Remove element
[Link]({5, 6}) # Union
[Link]({2, 3}) # Intersection

DICTIONARIES
d = {"a" : 1, "b" : 2}
d["a"] # Access value
[Link]( ) # Get keys
[Link]() # Get values
[Link]() # Get items

COMPREHENSIONS
[i*i for i in range (5) ] # List comp
{i for i in range(5)} # Set comp
{k:k*k for k in range(5)} # Dict comp

FILE HANDLING
with open("[Link]", "r") as f:
data = [Link]() # Read file

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


[Link]("Hello") # Write file

AKASH " PYTHON & TECH ENTHUSIAST ALL NOTES ARE UPLOADED ON TELEGRAM
@[Link] CHECK THE LINK IN BIO
python' (sWIPEE

MODULES &PACKAGES
import math
[Link](16) # Import module
from math import pi
print (pi) # Import specific

EXCEPTIONS
try:
X = 10/0
except ZeroDivisionError:
print("Cannot divide by zero") # Handle error
finally :
print(" Done") # Always runs

EXCEPTIONS
class Person:
def init__(self, name):
[Link] = name # Constructor
def greet(self):
return "Hello " + [Link] # Method

p = Person( "Akash")
[Link]()

[Link] &TECH ENTHUSIAST ALL NOTES ARE UPLOADED ON TELEGRAM


@[Link] CHECK THE LINK IN BIO

You might also like