Python commands cheat sheet with examples github

Miranda 193 Published: 11/25/2024

Python commands cheat sheet with examples github

Python Commands Cheat Sheet with Examples on GitHub

This cheat sheet provides a comprehensive guide to Python programming language, featuring essential commands and their practical applications. The document is designed as a reference for both beginners and experienced programmers.

Basic Syntax and Data Types Indentation: In Python, indentation (spaces or tabs) is used to define the block of code. Typically, 4 spaces are used per level of indentation.
if True:

print("This is a conditional statement")

Variables: Assigning values: Assign a value to a variable using the = operator.
name = "John"

age = 30

Data types: Python supports various data types, including strings (e.g., "hello"), integers (e.g., 1), floats (e.g., 3.14), and booleans (e.g., True, False).
my_string = "Hello"

my_int = 5

my_bool = True

Operators: Python supports various operators for performing arithmetic, comparison, logical, and assignment operations.
a = 2 + 3  # Arithmetic operator

b = a > 4 # Comparison operator

c = not b # Logical operator (not)

d = c or True # Logical operator (or)

Control Structures Conditional statements: if-else: Execute code if a condition is true; otherwise, execute alternative code.
x = 5

if x > 10:

print("x is greater than 10")

else:

print("x is less than or equal to 10")

for loops: Repeat execution of a block of code for each item in an iterable (e.g., list, tuple).
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

print(fruit)

while loops: Execute a block of code until a specified condition is met.
i = 0

while i < 5:

print(i)

i += 1

Functions: Define reusable blocks of code that can take arguments and return values.
def greet(name):

print(f"Hello, {name}!")

greet("John")

Functions and Modules Function arguments: Pass values to functions using the = operator.
def add(a, b):

return a + b

result = add(2, 3)

print(result) # Output: 5

Module importing: Use the import statement to bring external modules into your script.
import math

print(math.pi) # Output: 3.14159265359

File Input/Output and Exception Handling Reading files: Use the open() function to read from a file, specifying the mode ('r', 'w', etc.).
with open("example.txt", "r") as file:

content = file.read()

print(content) # Output: contents of example.txt

Writing files: Use the open() function to write to a file, specifying the mode ('w', 'a', etc.).
with open("example.txt", "w") as file:

file.write("Hello, Python!")

Exception handling: Catch and handle exceptions using try-except blocks.
try:

x = 5 / 0

except ZeroDivisionError:

print("Cannot divide by zero!")

else:

print(x) # Output: None (because it raised an exception)

GitHub Repository

The Python commands cheat sheet, along with its examples and explanations, is available on GitHub as a repository. The link to the repository is Python Commands Cheat Sheet.

Contribute to the Repository

This document is designed for sharing and collaboration. If you would like to contribute or suggest changes, please visit the repository's Issues page or submit pull requests.

Credits

The Python commands cheat sheet has been developed by a community of contributors from various programming backgrounds. Special thanks go to [your-username] for initiating and maintaining this document.

Python commands cheat sheet with examples pdf

I'd be happy to provide you with a comprehensive Python commands cheat sheet with examples in PDF format! Here it is:

Variables and Data Types

x = 5: Assigns the value 5 to a variable named x. y = "hello": Assigns the string "hello" to a variable named y. z = [1, 2, 3]: Assigns a list of integers to a variable named z.

w = {"a": 1, "b": 2}: Assigns a dictionary (or hash) to a variable named w.

Control Flow

if x > 5: print("x is greater than 5"): Checks if the value of x is greater than 5 and prints a message if true. while z: print(z.pop(0)): Loops through each element in list z, printing each one as it goes.

Functions

def add(x, y): return x + y: Defines a function that takes two arguments, x and y, and returns their sum. print(add(2, 3)): Calls the function with arguments 2 and 3, printing the result (5).

List Operations

z = [1, 2, 3]; print(z[0]): Accesses the first element of list z. w.append(4): Adds an element to the end of a list.

Dictionary Operations

d = {"a": 1, "b": 2}; print(d["a"]): Accesses a value from dictionary d. del d["b"]: Removes a key-value pair from dictionary d.

File Input/Output

f = open("example.txt", "w"): Opens a file for writing, creating it if necessary. f.write("Hello, world!"): Writes the string "Hello, world!" to the file. f.close() : Closes the file.

Math and Statistics

import math; print(math.pi): Imports the math module and prints the value of pi. print(sum(z)): Calculates the sum of all elements in list z.

Error Handling

try: x = int(input("Enter a number: ")) except ValueError: print("That's not a valid number!"): Tries to convert user input into an integer, catching any invalid input and printing an error message instead.

Miscellaneous

print(type(x)): Prints the type of variable x (e.g., "int" for an integer). import random; print(random.randint(1, 100)): Imports the random module and prints a random number between 1 and 100.

Here's the PDF version: Python Commands Cheat Sheet with Examples

I hope this helps!