Python Basics Cheat Sheet

In Python

Python is a high-level programming language that is widely used in various fields such as web development, data science, artificial intelligence, and more. It is known for its simplicity, readability, and ease of use, making it a popular choice for beginners and experts alike.

Here are some basic concepts of Python that every beginner should know:

1. Variables: Variables are used to store data in Python. They can hold different types of data such as numbers, strings, and lists.

2. Data Types: Python has several built-in data types such as integers, floats, strings, and booleans. Each data type has its own set of operations and methods.

3. Control Structures: Control structures are used to control the flow of a program. Python has if-else statements, loops, and functions that allow you to execute code based on certain conditions.

4. Functions: Functions are reusable blocks of code that perform a specific task. They can take input parameters and return output values.

5. Modules: Python has a vast library of modules that can be imported into your code to extend its functionality. Some popular modules include NumPy, Pandas, and Matplotlib.

6. Object-Oriented Programming: Python supports object-oriented programming, which allows you to create classes and objects that encapsulate data and behavior.

Let’s take a look at the cheat sheet below. Enjoy!

Variables and Data Types

SyntaxDescription
x = 5Assigns the value 5 to the variable x
y = ""Hello""Assigns the string ""Hello"" to the variable y
z = TrueAssigns the boolean value True to the variable z
a = [1, 2, 3]Assigns a list of integers to the variable a
b = {""name"": ""John"", ""age"": 30}Assigns a dictionary to the variable b

Operators

SyntaxDescription
+Addition
-Subtraction
*Multiplication
/Division
//Floor division
%Modulus
**Exponentiation
=Assignment
==Equality
!=Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
andLogical and
orLogical or
notLogical not

Control Flow

Conditional Statements

SyntaxDescription
if condition:Executes the code block if the condition is true
elif condition:Executes the code block if the previous condition(s) are false and this condition is true
else:Executes the code block if all previous conditions are false

Loops

SyntaxDescription
for variable in iterable:Executes the code block for each item in the iterable
while condition:Executes the code block while the condition is true

Functions

SyntaxDescription
def function_name(parameters):Defines a function with the given name and parameters
return valueReturns the value from a function

Input and Output

SyntaxDescription
print(value)Prints the value to the console
input(prompt)Prompts the user for input and returns the entered value

Modules

SyntaxDescription
import module_nameImports the module with the given name
from module_name import function_nameImports the specified function from the module
as aliasRenames the imported module or function with the given alias

Exceptions

SyntaxDescription
try:Executes the code block
except exception_type:Executes the code block if the specified exception is raised
finally:Executes the code block regardless of whether an exception was raised

Classes

SyntaxDescription
class class_name:Defines a class with the given name
def __init__(self, parameters):Defines the constructor for the class
def method_name(self, parameters):Defines a method for the class

Conclusion

This cheat sheet covers the basics of Python programming, including variables and data types, operators, control flow, functions, input and output, modules, exceptions, and classes. With this knowledge, you can start writing your own Python programs and exploring the vast world of Python libraries and frameworks.

Reference:

https://docs.python.org/3/