Featured image of post Python Language Cheatsheet

Python Language Cheatsheet

Python Language Cheatsheet

Python Cheatsheet

ConceptSyntax/ExampleDescription
Variablesx = 5Assigning a value to a variable
Data Typesint, float, str, list, tuple, dictCommon data types in Python
Listsmy_list = [1, 2, 3]Creating a list
Tuplesmy_tuple = (1, 2, 3)Creating a tuple
Dictionariesmy_dict = {"key": "value"}Creating a dictionary
Functionsdef my_function():Defining a function
If Statementsif x > 0:Basic if statement
Loopsfor i in range(5):For loop
While Loopswhile x < 5:While loop
List Comprehension[x for x in range(5)]List comprehension
Importing Modulesimport mathImporting a module
Classesclass MyClass:Defining a class
Inheritanceclass ChildClass(ParentClass):Defining a child class
Exception Handlingtry: ... except: ...Handling exceptions
File I/Owith open('file.txt', 'r') as file:Reading from a file
Lambda Functionlambda x: x + 1Anonymous function