Skip to content

jkingportfolio/python-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

Python Cheatsheet My Skills

Welcome to the Python Cheatsheet repository! This repository serves as a comprehensive reference guide for Python, covering the core language features, syntax, standard libraries, and common programming patterns. Whether you're a beginner learning Python or an experienced developer seeking a quick reference, this cheatsheet is here to assist you.

The cheatsheet includes various sections dedicated to Python fundamentals, data types, control flow, functions, modules, file I/O, exceptions, object-oriented programming, regular expressions, and more. Each section provides concise explanations and code examples to help you grasp Python concepts and write clean and efficient code.

Feel free to explore this cheatsheet to enhance your Python skills, automate tasks, build applications, or delve into data science and machine learning. The cheatsheet is written in Markdown format, making it easy to view, copy, and paste into your own projects.

If you have any suggestions or would like to contribute to this cheatsheet, please feel free to open an issue or submit a pull request. Let's collaborate and make this cheatsheet a valuable resource for the Python developer community!

Happy coding with Python!


Table of Contents

  1. About
  2. Uses
  3. Installation
  4. Dependencies and Creating a requirements File
  5. Syntax
  6. Variables
  7. Data Types
  8. Operators
  9. Conditions
  10. Control Flow Statements
  11. Collections
  12. Functions
  13. Lambda Functions
  14. List Comprehensions
  15. Error Handling (try-except)
  16. Object-Oriented Programming (OOP) Concepts
  17. Decorators
  18. Generators
  19. File Handling
  20. Regular Expressions
  21. Date and Time
  22. JSON Handling
  23. CSV Handling
  24. Exception Hierarchy
  25. Virtual Environments
  26. Package Installation with pip
  27. Command-Line Arguments
  28. Multithreading
  29. Multiprocessing
  30. Context Managers (with statement)
  31. Recursion
  32. Web Scraping
  33. Data Serialization (e.g., Pickle)
  34. Database Connectivity (e.g., SQLite, MySQL)
  35. Set Methods
  36. Mathematical Functions (math module)
  37. Random Number Generation (random module)
  38. Working with Files and Directories (os module)
  39. Command Execution (subprocess module)
  40. Regular Expression Operations (re module)
  41. Handling Dates and Times (datetime module)
  42. Working with CSV Files (csv module)
  43. Working with JSON Data (json module)
  44. Data Compression and Archiving (zipfile module)
  45. Networking and Internet Communication (socket module)
  46. Sending Emails (smtplib module)
  47. Web Development (Flask, Django)
  48. Scraping Websites (BeautifulSoup, Scrapy)
  49. Data Visualization (Matplotlib, Seaborn)
  50. Scientific Computing (NumPy, SciPy)
  51. Machine Learning (scikit-learn, TensorFlow)
  52. Data Analysis and Manipulation (pandas)
  53. Working with Images (PIL/Pillow module)
  54. GUI Development (tkinter, PyQt)
  55. Unit Testing (unittest module)

About

Welcome to the Python Cheat Sheet! This cheat sheet is designed to provide quick and handy reference to various Python concepts, syntax, and libraries. Whether you're a beginner learning Python or an experienced developer looking for a quick reminder, this cheat sheet aims to be your go-to resource.

What is Python?

Python is a versatile, high-level programming language known for its simplicity and readability. It supports multiple programming paradigms, making it suitable for various applications, including web development, data analysis, machine learning, scientific computing, and more.

Why Use This Cheat Sheet?

This cheat sheet aims to help you:

  • Quickly find syntax examples for various Python constructs.
  • Refresh your memory on essential Python concepts.
  • Explore a wide range of topics, from basic programming concepts to advanced libraries.

Example Code

Here's a simple example of Python code that prints "Hello, Python!" to the console:

print("Hello, Python!")

In the above code:

print is a built-in function used to display output. "Hello, Python!" is a string, enclosed in double quotes. Feel free to explore the different sections of this cheat sheet to deepen your understanding of Python!

Uses

Python is a versatile language that finds applications in various domains. Here are some common uses of Python:

1. Web Development

Python is used to build dynamic and interactive websites using frameworks like Flask and Django. These frameworks simplify the process of creating web applications by providing tools for handling routing, databases, and user authentication.

2. Data Analysis and Visualization

Python's libraries such as pandas and Matplotlib are widely used for data analysis and visualization tasks. You can load, manipulate, and analyze large datasets and create insightful visualizations.

3. Machine Learning and Artificial Intelligence

Python is a go-to language for machine learning and AI projects. Libraries like scikit-learn, TensorFlow, and PyTorch provide tools for creating and training machine learning models.

4. Scientific Computing

Python is used in scientific research and simulations due to its easy syntax and libraries like NumPy and SciPy, which provide support for mathematical and scientific computations.

5. Automation and Scripting

Python is often used for automating repetitive tasks and writing scripts to perform various operations, making it a valuable tool for sysadmins and DevOps professionals.

6. Game Development

Python can be used to develop simple 2D games using libraries like Pygame, making it a great choice for beginners interested in game development.

7. Internet of Things (IoT)

Python's lightweight nature and libraries like MicroPython make it suitable for programming IoT devices, such as microcontrollers and single-board computers.

Example Code

Here's a simple example of how Python can be used for data analysis using the pandas library:

import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 22]}
df = pd.DataFrame(data)

# Display the DataFrame
print(df)

In the above code:

import pandas as pd imports the pandas library and assigns it the alias pd.
pd.DataFrame(data) creates a DataFrame from the given data.
print(df) displays the DataFrame.

Installation

Before you start coding in Python, you need to install the Python interpreter on your system. Here's how you can do it:

Installing Python

  1. Download: Visit the official Python website to download the latest version of Python.

  2. Run Installer: Run the downloaded installer and follow the installation wizard's instructions. Make sure to check the option to add Python to your system's PATH during installation.

  3. Verification: Open a command prompt (or terminal) and type python --version to verify that Python has been installed successfully. You should see the installed version number.

Using the Python Interpreter

Once Python is installed, you can use the Python interpreter to run your code.

Interactive Mode

To start the interactive mode, simply open a command prompt (or terminal) and type python. This allows you to enter Python code directly and see the output immediately.

$ python
Python 3.9.6 (default, Jun 29 2021, 06:20:32)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, Python!")
Hello, Python!
>>>

To exit the interactive mode, type exit() or press Ctrl + Z (Windows) or Ctrl + D (Unix-like systems).

Running Python Scripts To run a Python script saved in a file (e.g., my_script.py), open a command prompt (or terminal) and navigate to the folder containing the script. Then, run the script using the command:

python my_script.py

Example Code Here's a simple "Hello, World!" script:

print("Hello, World!")

In the above code, print("Hello, World!") displays the message "Hello, World!" on the console.

Now that you have Python installed, you're ready to start coding in this versatile language!

Dependencies and Creating a requirements File

You should create a requirements.txt file for your Python project as soon as you start adding external dependencies to your project. This ensures that you maintain a record of the specific packages and their versions that your project depends on. Here are some guidelines on when to create a requirements.txt file:

When You Add External Packages: As soon as you start using external Python packages in your project, it's a good idea to create a requirements.txt file. This helps you keep track of the dependencies and their versions, making it easier to recreate the same development environment later.

When Collaborating with Others: If you're collaborating with other developers, a requirements.txt file becomes even more important. It ensures that everyone working on the project is using the same set of dependencies, reducing the chances of compatibility issues.

Before Deploying or Sharing Your Code: Before deploying your project to production or sharing it with others, you should update your requirements.txt file to include all the necessary dependencies. This ensures that others can set up the same environment and run your code without any issues.

Regular Maintenance: It's a good practice to update your requirements.txt file whenever you add, update, or remove dependencies. This keeps the file accurate and up to date with the current state of your project's dependencies.

Using Virtual Environments: If you're using virtual environments (which is recommended), it's a good idea to create a requirements.txt file as soon as you've set up your virtual environment and installed the necessary packages within that environment.

In essence, creating a requirements.txt file early in your project's development helps establish good practices and ensures that you have a clear record of your project's dependencies. This makes it easier to manage the development, collaboration, and deployment of your Python project.

Using requirements.txt to Install Dependencies: Once you have a requirements.txt file listing all the required packages and their versions, you can easily set up your development environment on another system by following these steps:

  1. Create a Virtual Environment (Recommended):

    python -m venv .venv
    source .venv/bin/activate   # On macOS and Linux
    .\venv\Scripts\activate    # On Windows
  2. Install Dependencies from requirements.txt:

    pip install -r requirements.txt
  3. Verify Installed Packages: After installation, you can verify that the packages are installed by running:

    pip list
  4. Deactivate the Virtual Environment: When you're done working with your project, you can deactivate the virtual environment:

    deactivate

Using the requirements.txt file in this way helps ensure that your development environment matches the one you used to develop your project. It's a convenient method to set up and manage dependencies, especially when sharing your code with others or deploying it to different systems.

Remember to keep your requirements.txt file up to date as you add or modify dependencies during the course of your project's development.

Syntax

Python's syntax is known for its simplicity and readability. Let's explore some of the basic syntax rules.

Indentation

Unlike many other programming languages that use curly braces or keywords to indicate blocks of code, Python uses indentation. Indentation is crucial for defining code blocks and maintaining readability.

if True:
    print("This is indented correctly")
else:
    print("This is also indented correctly")

Comments

Comments are used to add explanations or notes to your code and are ignored by the Python interpreter.

This is a single-line comment

""" This is a multi-line comment """

Statements and Lines Python statements are typically written one per line. However, you can use a backslash \ to continue a statement onto the next line.

total = 10 + \
        20 + \
        30

print(total)

Quotation Marks for Strings Strings can be enclosed in either single (' ') or double (" ") quotation marks.

single_quoted = 'This is a single-quoted string'
double_quoted = "This is a double-quoted string"

Example Code

Here's an example that demonstrates the basic syntax of Python:

# This is a simple Python program
name = "Alice"
age = 30

if age >= 18:
    print(name + " is an adult.")
else:
    print(name + " is a minor.")

In the above code:

name and age are variables.
if and else are control flow statements.
print() is a built-in function used to display output.
Understanding Python's syntax is the first step towards writing effective and readable code!

Variables

Variables are used to store and manage data in Python. They are like containers that hold different types of information.

Variable Naming Rules

  • Variable names can consist of letters (uppercase/lowercase), digits, and underscores.
  • They must start with a letter or an underscore (_), followed by letters, digits, or underscores.
  • Variable names are case-sensitive (e.g., myVar and myvar are different).

Assigning Values to Variables

You can assign values to variables using the assignment operator (=).

name = "Alice"
age = 30
height = 5.8
is_student = True

Data Types

Python has various built-in data types:

Integers: Whole numbers, e.g., 5, -10.
Floats: Numbers with decimal points, e.g., 3.14, -0.5.
Strings: Sequences of characters, e.g., "Hello, Python!".
Booleans: Represents True or False values.
Lists: Ordered collections, e.g., [1, 2, 3], ["apple", "banana", "cherry"].
Tuples: Similar to lists, but immutable, e.g., (1, 2, 3).
Sets: Unordered collections with unique values, e.g., {1, 2, 3}.
Dictionaries: Key-value pairs, e.g., {"name": "Alice", "age": 30}.

In Python, data types define the kind of value a variable can hold. Here are some fundamental data types:

Numbers

Numbers in Python can be classified into two main categories: integers and floating-point numbers.

Integers

Integers are whole numbers without a decimal point. They can be positive, negative, or zero.

positive_integer = 42
negative_integer = -10
zero = 0

Floating-Point Numbers

Floating-point numbers (floats) represent numbers with decimal points.

positive_float = 3.14
negative_float = -0.5

Strings

Strings are sequences of characters, enclosed in single (' ') or double (" ") quotation marks.

single_quoted = 'This is a single-quoted string.'
double_quoted = "This is a double-quoted string."
multiline_string = '''This is a
multiline string.'''

You can perform various operations on strings, such as concatenation and slicing.

greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"

Booleans

Booleans represent two values: True or False. They are often used in conditions and logical operations.

is_adult = True
is_student = False

Example Code

Here's an example that demonstrates the usage of numbers, strings, and booleans in Python:

# Numbers
age = 25
height = 5.9

# Strings
greeting = "Hello"
name = "Bob"
message = greeting + ", " + name + "!"

# Booleans
is_qualified = True
is_employee = False

print("Age:", age)
print("Height:", height)
print("Message:", message)
print("Is qualified?", is_qualified)

In the above code:

Various types of numbers, strings, and booleans are declared and used. String concatenation is shown in the message variable. The print() function is used to display the values. Understanding these fundamental data types is essential for writing Python programs effectively!

Operators

Operators in Python are symbols that perform operations on variables and values. They are a fundamental part of programming, allowing you to manipulate data and perform calculations.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

x = 10
y = 3

addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
modulus = x % y
exponentiation = x ** y

print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Modulus:", modulus)
print("Exponentiation:", exponentiation)

Comparison Operators

Comparison operators are used to compare values.

a = 5
b = 10

equal = a == b
not_equal = a != b
greater_than = a > b
less_than = a < b
greater_than_equal = a >= b
less_than_equal = a <= b

print("Equal:", equal)
print("Not Equal:", not_equal)
print("Greater Than:", greater_than)
print("Less Than:", less_than)
print("Greater Than or Equal:", greater_than_equal)
print("Less Than or Equal:", less_than_equal)

Logical Operators

Logical operators are used to combine conditional statements.

p = True
q = False

logical_and = p and q
logical_or = p or q
logical_not = not p

print("Logical AND:", logical_and)
print("Logical OR:", logical_or)
print("Logical NOT:", logical_not)

Assignment Operators

Assignment operators are used to assign values to variables.

x = 10
x += 5  # Equivalent to x = x + 5
x -= 3  # Equivalent to x = x - 3
x *= 2  # Equivalent to x = x * 2
x /= 4  # Equivalent to x = x / 4
x %= 2  # Equivalent to x = x % 2
x **= 3  # Equivalent to x = x ** 3

print("Updated x:", x)

Example Code

Here's an example that demonstrates various types of operators in Python:

# Arithmetic Operators
a = 10
b = 3
result = a + b * 2

# Comparison Operators
x = 5
y = 7
is_equal = x == y

# Logical Operators
p = True
q = False
result = p and not q

print("Arithmetic Result:", result)
print("Comparison Result:", is_equal)
print("Logical Result:", result)

In the above code:

Arithmetic, comparison, and logical operators are demonstrated. The print() function is used to display the results. Operators enable you to perform a wide range of operations in your Python code!

Conditions

Conditional statements allow your program to make decisions and perform different actions based on certain conditions.

IF Statement

The if statement is used to execute a block of code only if a certain condition is true.

age = 18

if age >= 18:
    print("You are an adult.")

IF-ELSE Statement

The if-else statement is used to execute one block of code if the condition is true and another block if it's false.

temperature = 25

if temperature > 30:
    print("It's hot outside.")
else:
    print("It's not too hot.")

IF-ELIF-ELSE Statement

The if-elif-else statement is used when you have multiple conditions to check.

grade = 85

if grade >= 90:
    print("Excellent!")
elif grade >= 70:
    print("Good job.")
else:
    print("Keep working hard.")

Example Code

Here's an example that demonstrates the use of conditional statements in Python:

# IF Statement
x = 10
if x > 5:
    print("x is greater than 5")

# IF-ELSE Statement
y = 3
if y % 2 == 0:
    print("y is even")
else:
    print("y is odd")

# IF-ELIF-ELSE Statement
time = 18
if time < 12:
    print("Good morning!")
elif time < 18:
    print("Good afternoon!")
else:
    print("Good evening!")

In the above code:

Different types of conditional statements are demonstrated. The print() function is used to display the corresponding output. Conditional statements are essential for controlling the flow of your program based on different situations!

Control Flow Statements

Control flow statements allow you to control the execution of your program by making decisions based on conditions and looping through code blocks.

IF Statement

The if statement is used to execute a block of code only if a certain condition is true.

age = 18

if age >= 18:
    print("You are an adult.")

IF-ELSE Statement

The if-else statement is used to execute one block of code if the condition is true and another block if it's false.

temperature = 25

if temperature > 30:
    print("It's hot outside.")
else:
    print("It's not too hot.")

IF-ELIF-ELSE Statement

The if-elif-else statement is used when you have multiple conditions to check.

grade = 85

if grade >= 90:
    print("Excellent!")
elif grade >= 70:
    print("Good job.")
else:
    print("Keep working hard.")

WHILE Loop

The while loop is used to repeatedly execute a block of code as long as the condition is true.

count = 0
while count < 5:
    print("Count:", count)
    count += 1

FOR Loop

The for loop is used to iterate over a sequence (e.g., a list, tuple, or string) and execute a block of code for each item.

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print("Fruit:", fruit)

Example Code

Here's an example that demonstrates the use of control flow statements in Python:

# IF Statement
x = 10
if x > 5:
    print("x is greater than 5")

# IF-ELSE Statement
y = 3
if y % 2 == 0:
    print("y is even")
else:
    print("y is odd")

# IF-ELIF-ELSE Statement
time = 18
if time < 12:
    print("Good morning!")
elif time < 18:
    print("Good afternoon!")
else:
    print("Good evening!")

# WHILE Loop
count = 0
while count < 3:
    print("Count:", count)
    count += 1

# FOR Loop
for num in range(3):
    print("Number:", num)

In the above code:

Different control flow statements are demonstrated. The print() function is used to display the corresponding output. Control flow statements allow you to create dynamic and flexible programs that respond to various conditions and iterations!

Collections

Collections in Python allow you to store multiple values in a single variable. There are several types of collections available.

Lists

Lists are ordered and mutable collections that can hold different data types.

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "apple", True]

You can access elements in a list by their index, and you can modify, add, or remove elements.

print(fruits[0])  # Outputs: "apple"
fruits[1] = "orange"
fruits.append("grape")
fruits.remove("cherry")

Tuples

Tuples are ordered and immutable collections. Once created, you can't modify their content.

coordinates = (3, 5)
person = ("Alice", 30, "female")

Accessing elements in a tuple is similar to accessing elements in a list.

print(person[0])  # Outputs: "Alice"

Sets

Sets are unordered collections that store unique values.

colors = {"red", "green", "blue"}
fruits = {"apple", "banana", "apple", "cherry"}  # Duplicates are ignored

You can perform various set operations like union, intersection, and difference.

new_colors = {"yellow", "blue"}
all_colors = colors.union(new_colors)

Dictionaries

Dictionaries are collections of key-value pairs. They are unordered and mutable.

person = {
    "name": "Alice",
    "age": 30,
    "gender": "female"
}

You can access values using keys and update them.

print(person["name"])  # Outputs: "Alice"
person["age"] = 31

Example Code

Here's an example that demonstrates the use of various collection data types in Python:

# Lists
fruits = ["apple", "banana", "cherry"]
print(fruits[1])

# Tuples
coordinates = (3, 5)
print(coordinates[0])

# Sets
colors = {"red", "green", "blue"}
print("green" in colors)

# Dictionaries
person = {"name": "Bob", "age": 25}
print(person["age"])

In the above code:

Different collection types are demonstrated with sample operations. The print() function is used to display the output. Collections provide efficient ways to store and manage multiple values in your Python programs!

Functions

Functions are blocks of reusable code that can be called with different inputs to perform specific tasks. They help in organizing and modularizing code.

Defining Functions

In Python, you define a function using the def keyword, followed by the function name and parentheses containing parameters. The code block indented under the function definition is the function's body.

def greet(name):
    print("Hello, " + name + "!")

Calling Functions

Once a function is defined, you can call it by using its name followed by parentheses, passing in the required arguments.

greet("Alice")
greet("Bob")

Return Statement

Functions can return values using the return statement. This allows you to capture and use the result of the function's computation.

def add(x, y):
    return x + y

result = add(5, 3)
print("Sum:", result)

Default Arguments

You can provide default values for function parameters. If a value is not provided when calling the function, the default value is used.

def power(base, exponent=2):
    return base ** exponent

print(power(3))        # Outputs: 9
print(power(2, 3))     # Outputs: 8

Example Code

Here's an example that demonstrates the use of functions in Python:

# Defining a Function
def square(x):
    return x ** 2

# Calling Functions
result = square(4)
print("Square:", result)

# Using a Function with Default Argument
def greet(name, greeting="Hello"):
    print(greeting + ", " + name + "!")

greet("Alice")
greet("Bob", "Hi")

In the above code:

A function is defined to calculate the square of a number. Functions are called with various arguments. A function with a default argument is demonstrated. Functions are essential for writing modular and organized code that can be reused across your program.

Lambda Functions

Lambda functions, also known as anonymous functions, are small, unnamed functions that can have any number of arguments but only one expression. They are often used for short operations.

Syntax

Lambda functions are defined using the lambda keyword, followed by the arguments and the expression. The result of the expression is automatically returned.

# Syntax: lambda arguments: expression
square = lambda x: x ** 2

Using Lambda Functions

Lambda functions can be assigned to variables and used just like regular functions.

# Using the lambda function
result = square(4)  # Outputs: 16

Lambda with Other Functions

Lambda functions are commonly used with other functions like map(), filter(), and sorted().

numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

Example Code

Here's an example that demonstrates the use of lambda functions in Python:

# Defining a Lambda Function
multiply = lambda x, y: x * y

# Using the Lambda Function
result = multiply(3, 5)
print("Result:", result)

# Using Lambda with map()
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print("Squared Numbers:", squared_numbers)

In the above code:

A lambda function is defined to multiply two numbers. The lambda function is used with the map() function to square numbers. Lambda functions are handy for writing short and concise operations in your code.

List Comprehensions

List comprehensions provide a concise way to create lists by applying an expression to each item in an iterable (e.g., a list or range).

Syntax

The basic syntax of a list comprehension is:

new_list = [expression for item in iterable]

Using List Comprehensions

List comprehensions are used to create new lists with transformed or filtered elements from an existing iterable.

numbers = [1, 2, 3, 4, 5]
squared_numbers = [x ** 2 for x in numbers]
even_numbers = [x for x in numbers if x % 2 == 0]

Nested List Comprehensions

You can use nested list comprehensions to create lists of lists or apply multiple transformations.

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [x for row in matrix for x in row]

Example Code

Here's an example that demonstrates the use of list comprehensions in Python:

# Using List Comprehensions
numbers = [1, 2, 3, 4, 5]
squared_numbers = [x ** 2 for x in numbers]
even_numbers = [x for x in numbers if x % 2 == 0]

print("Original Numbers:", numbers)
print("Squared Numbers:", squared_numbers)
print("Even Numbers:", even_numbers)

# Nested List Comprehensions
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [x for row in matrix for x in row]
print("Flattened Matrix:", flattened)

In the above code:

List comprehensions are used to create new lists based on existing data. Nested list comprehensions are used to flatten a matrix. List comprehensions are a powerful way to simplify your code when working with iterables and creating new lists.

Virtual Environments

Setting up new projects within a folder called "Coding" on your desktop and using a virtual environment for each project is a good practice in software development. This approach helps keep your projects organized, isolated, and prevents dependency conflicts between different projects. Here's a step-by-step guide on how to achieve this:

  1. Create a Folder on Desktop:

    1.1. Right-click on your desktop and select "New" > "Folder".

    1.2. Name the folder "Coding" or any other suitable name you prefer.

  2. Set Up New Projects:

    2.1. Open the "Coding" folder you just created.

    2.2. Create a new subfolder for each project. Name these subfolders based on your project names (e.g., "ProjectA," "ProjectB").

  3. Virtual Environments:

    A virtual environment is an isolated environment for Python projects that allows you to manage dependencies separately for each project. Follow these steps to set up a virtual environment for each project:

    3.1. Open a command-line interface (Terminal, Command Prompt, etc.).

    3.2. Navigate to the directory of your project within the "Coding" folder:

    cd path\to\Desktop\Coding\ProjectA

    3.3. Create a virtual environment for your project:

    python -m venv <name_of_virtual_environment>

    Common convention is to name your virtual environment .venv

    python -m venv .venv

    3.4. Activate the virtual environment:

    On Windows:

    .venv\Scripts\activate

    (NOTE: replace ".venv" with your virtual environments name if you have chosen something other than the common convention naming)

    On macOS and Linux:

    source .venv/bin/activate

    3.5. You'll notice that the command prompt changes, indicating that you're now within the virtual environment.

    3.6. Install project-specific dependencies within this virtual environment using pip:

    pip install package-name

    3.7 To deactivate the virtual environment when you're done working on the project, simply enter:

    deactivate
  4. Repeat for Each Project:

    Repeat steps 3.2 to 3.7 for each new project you create within the "Coding" folder. Each project will have its own isolated environment and dependencies.

  5. Working with Projects:

    8.1. Whenever you want to work on a specific project, navigate to its folder and activate its virtual environment as shown in step 3.4.

    8.2. When you switch between projects, deactivate the current virtual environment (step 3.7) before activating the new one.

By following these steps, you ensure that each project has its own isolated environment, preventing dependency clashes and making it easier to manage and collaborate on multiple projects within the "Coding" folder on your desktop.

About

Cheat / Help sheet repo for Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published