icon picker
Universal Coding Helper

Generate code helper for the language of your choice
info
Select the language and press Refresh All to create a new cheat sheet
Users can add the languages of their choice to the language selection drop down list. Also restructure the sheet , add/remove functionalities of their choice to make it more useful for their everyday work

Select language

Blank

Refresh All

Data Types

Integer: 5
Float: 3.14
Boolean: True
String: “Hello, world!”
List: [1, 2, 3]
Tuple: (4, 5, 6)
Dictionary: {"name": "John", "age": 30}
Set: {1, 2, 3}

Conditional Operators

Conditional operators in Python are used to create conditional statements that evaluate to either True or False. The three main conditional operators in Python are:
== (equal to): checks if two values are equal
!= (not equal to): checks if two values are not equal
<, >, <=, >= (less than, greater than, less than or equal to, greater than or equal to): compare two values numerically
These operators are commonly used in if-else statements and while loops to control program flow based on certain conditions.

Type Conversions

Type conversion in Python can be done using built-in functions such as int(), float(), str(), and bool(). Here are some examples:
Converting a string to an integer: int('42') will return 42
Converting a float to an integer: int(3.14) will return 3
Converting an integer to a string: str(123) will return ’123'
Converting a boolean to a string: str(True) will return ’True'
Note that type conversion may result in errors if the value being converted is not compatible with the new data type.

File Operations

Opening a file: file = open("example.txt", "r")
Reading a file: content = file.read()
Writing to a file: file.write(”This is some text.")
Closing a file: file.close()

Main Built Functions

print(): prints the specified message to the screen
len(): returns the length of an object (string, list, dictionary, etc.)
type(): returns the type of an object
range(): returns a sequence of numbers, starting from 0 by default, and increments by 1 (or specified value)
str(): converts an object into a string
int(): converts an object into an integer
float(): converts an object into a floating-point number
list(): creates a list
dict(): creates a dictionary
set(): creates a set

Useful Resources

Error Messages

NameError: name ‘variable_name’ is not defined
SyntaxError: invalid syntax
TypeError: unsupported operand type(s) for +: 'int' and ‘str’
ValueError: invalid literal for int() with base 10: 'non-numeric value’

Debugging

Debugging in Python involves identifying and fixing errors or bugs in code. One common method is to use print statements to check the value of variables at different points in the code. Another method is to use a debugger, such as pdb, which allows for interactive inspection of the code. Here is an example of using a print statement for debugging:

x = 5
y = 2

# incorrect calculation
z = x / y

print(z) # should be 2.5
In this example, the calculation for z is incorrect, so we can use the print statement to check its value and identify the error.

Arithmetic Operators

Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulo: %
Floor division: //

Collection

List: my_list = [item1, item2, item3]
Tuple: my_tuple = (item1, item2, item3)
Set: my_set = {item1, item2, item3}
Dictionary: my_dict = {'key1': item1, 'key2': item2, ‘key3': item3}

String Operations

String operations in Python refer to the various manipulations that can be performed on strings. These operations include concatenation, slicing, indexing, and formatting.
Here are some examples:
Concatenation: joining two or more strings together using the + operator.

string1 = "Hello"
string2 = "world"
result = string1 + " " + string2
print(result)

# Output: "Hello world"
Slicing: extracting a portion of a string using the [start:end] notation.

string = "Python is awesome"
result = string[0:6]
print(result)

# Output: "Python"
Indexing: accessing a specific character in a string using the [index] notation.

string = "Python"
result = string[2]
print(result)

# Output: "t"
Formatting: inserting values into a string using placeholders, such as {} and the .format() method.

name = "Alice"
age = 27
result = "My name is {} and I am {} years old".format(name, age)
print(result)

# Output: "My name is Alice and I am 27 years old"

Functions

Functions are used in Python programming to encapsulate code that can be reused multiple times. They allow for modular programming and make the code more readable and organized. Here are some examples:
Built-in functions like print(), len(), and range() are used to perform common tasks.
User-defined functions can be created to perform specific tasks. For example, a function to calculate the area of a circle can be defined as follows:
python
def calculate_area(radius):
pi = 3.14
area = pi * radius ** 2
return area
Lambda functions can be used to create anonymous functions that can be passed as arguments to other functions. For example, the map() function can be used with a lambda function to apply a transformation to each element in a list:
python
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]


Logical Operators

and: a logical operator that returns True if both operands are True
or: a logical operator that returns True if at least one operand is True
not: a logical operator that returns the opposite boolean value of the operand

Control Flow

Python provides a variety of control flow methods, including conditional statements and loops.
One example of a conditional statement is the if-else statement, which allows you to execute different code blocks depending on whether a condition is true or false. For example:

x = 5
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
This code will output “x is less than or equal to 10", since the condition in the if statement (x > 10) is false.
Another control flow method in Python is the for loop, which allows you to iterate over a collection of items. For example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
This code will output:

apple
banana
cherry
Finally, the while loop allows you to repeatedly execute a block of code as long as a condition is true. For example:

i = 0
while i < 5:
print(i)
i += 1
This code will output:

0
1
2
3
4
These are just a few examples of the control flow methods available in Python. By using these tools, you can create powerful and flexible programs that respond to changing conditions and user inputs.

Exception Handling

Exception handling in Python with samples

Exception handling in Python is a way to handle errors that might occur during the execution of a program. It allows the program to continue running and handle the error in a controlled manner. Here are some examples of how to handle exceptions in Python:

Example 1: Try-Except Block

python
try:
# code that might raise an exception
except ExceptionType:
# code to handle the exception

Example 2: Raise Exception

python
if condition:
raise Exception("Error message")

Example 3: Finally Block

python
try:
# code that might raise an exception
finally:
# code that will always be executed
For more information on exception handling in Python, please refer to the .


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.