Python Cheat Sheet 🎯

beginner
12 min

Python Cheat Sheet 🎯

Welcome to the Python Cheat Sheet! This guide is designed to help you navigate the exciting world of Python programming, suitable for beginners and intermediates alike. Let's dive in!

Python Essentials 📝

Variables and Data Types

Variables are containers that hold data. Python has several built-in data types:

  • Numbers: Integers (int), Floating-point numbers (float), and Complex numbers (complex)
  • Strings (str): Sequence of characters
  • Booleans (bool): True or False
  • Lists (list): Ordered, mutable collection of elements
  • Tuples (tuple): Ordered, immutable collection of elements
  • Sets (set): Unordered, mutable collection of unique elements
  • Dictionaries (dict): Unordered, mutable collection of key-value pairs
python
# Examples of variables and data types num = 5 # Integer float_num = 5.0 # Floating-point number complex_num = 3+4j # Complex number str_var = "Hello, World!" # String bool_var = True # Boolean list_var = [1, 2, 3] # List tuple_var = (1, 2, 3) # Tuple set_var = {1, 2, 3} # Set dict_var = {"key": "value"} # Dictionary

Basic Operations ✅

Arithmetic Operations

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulo (Remainder): %
  • Exponentiation: **

Comparison Operators

  • Equal: ==
  • Not Equal: !=
  • Greater Than: >
  • Less Than: <
  • Greater Than or Equal: >=
  • Less Than or Equal: <=

Control Structures 💡

Conditional Statements

If-Else

python
if condition: # If block elif condition: # If-Else block else: # Else block

Ternary Operator

python
value = condition if condition_is_true else condition_is_false

Loops

For Loop

python
for element in iterable: # Loop body

While Loop

python
while condition: # Loop body

Functions 📝

A function is a block of code that can be called by name. Here's how to define a function in Python:

python
def function_name(parameters): # Function body

Modules and Packages 💡

Python organizes code into modules and packages to promote reusability and modularity. You can import a module using the import statement:

python
import module_name

Quiz 📝

Quick Quiz
Question 1 of 1

What is the correct syntax for defining a function in Python?

Practical Applications 💡

  • Data Analysis: With libraries like pandas, NumPy, and Matplotlib, you can perform data analysis, manipulation, and visualization.
  • Web Development: With frameworks like Django and Flask, you can build dynamic and scalable web applications.
  • Machine Learning: Python is widely used for machine learning and AI thanks to libraries like TensorFlow, Keras, and Scikit-learn.