Cheat Sheet: Python Basics
Package/Method | Description | Code Example |
---|---|---|
Comments | Comments are lines of text that are ignored by the Python interpreter when executing the code<./td> |
|
Concatenation | Combines (concatenates) strings. | Syntax:
Example:
|
Data Types | - Integer - Float - Boolean - String | Example:
|
Indexing | Accesses character at a specific index. | Example:
|
len() | Returns the length of a string. | Syntax:
Example:
|
lower() | Converts string to lowercase. | Example:
|
print() | Prints the message or variable inside `()`. | Example:
|
Python Operators | - Addition (+): Adds two values together. - Subtraction (-): Subtracts one value from another. - Multiplication (*): Multiplies two values. - Division (/): Divides one value by another, returns a float. - Floor Division (//): Divides one value by another, returns the quotient as an integer. - Modulo (%): Returns the remainder after division. | Example:
|
replace() | Replaces substrings. | Example:
|
Slicing | Extracts a portion of the string. | Syntax:
Example:
|
split() | Splits string into a list based on a delimiter. | Example:
|
strip() | Removes leading/trailing whitespace. | Example:
|
upper() | Converts string to uppercase. | Example:
|
Variable Assignment | Assigns a value to a variable. | Syntax:
Example:
|
List
Package/Method | Description | Code Example |
---|---|---|
append() | The `append()` method is used to add an element to the end of a list. | Syntax:
Example:
|
copy() | The `copy()` method is used to create a shallow copy of a list. | Example 1:
|
count() | The `count()` method is used to count the number of occurrences of a specific element in a list in Python. | Example:
|
Creating a list | A list is a built-in data type that represents an ordered and mutable collection of elements. Lists are enclosed in square brackets [] and elements are separated by commas. | Example:
|
del | The `del` statement is used to remove an element from list. `del` statement removes the element at the specified index. | Example:
|
extend() | The `extend()` method is used to add multiple elements to a list. It takes an iterable (such as another list, tuple, or string) and appends each element of the iterable to the original list. | Syntax:
Example:
|
Indexing | Indexing in a list allows you to access individual elements by their position. In Python, indexing starts from 0 for the first element and goes up to `length_of_list - 1`. | Example:
|
insert() | The `insert()` method is used to insert an element. | Syntax:
Example:
|
Modifying a list | You can use indexing to modify or assign new values to specific elements in the list. | Example:
|
pop() | `pop()` method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don't provide an index to the `pop()` method, it will remove and return the last element of the list by default | Example 1:
Example 2:
|
remove() | To remove an element from a list. The `remove()` method removes the first occurrence of the specified value. | Example:
|
reverse() | The `reverse()` method is used to reverse the order of elements in a list | Example 1:
|
Slicing | You can use slicing to access a range of elements from a list. | Syntax:
Example:
|
sort() | The `sort()` method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass the `reverse=True` argument to the `sort()` method. | Example 1:
Example 2:
|
Dictionary
Package/Method | Description | Code Example |
---|---|---|
Accessing Values | You can access the values in a dictionary using their corresponding `keys`. | Syntax:
Example:
|
Add or modify | Inserts a new key-value pair into the dictionary. If the key already exists, the value will be updated; otherwise, a new entry is created. | Syntax:
Example:
|
clear() | The `clear()` method empties the dictionary, removing all key-value pairs within it. After this operation, the dictionary is still accessible and can be used further. | Syntax:
Example:
|
copy() | Creates a shallow copy of the dictionary. The new dictionary contains the same key-value pairs as the original, but they remain distinct objects in memory. | Syntax:
Example:
|
Creating a Dictionary | A dictionary is a built-in data type that represents a collection of key-value pairs. Dictionaries are enclosed in curly braces `{}`. | Example:
|
del | Removes the specified key-value pair from the dictionary. Raises a `KeyError` if the key does not exist. | Syntax:
Example:
|
items() | Retrieves all key-value pairs as tuples and converts them into a list of tuples. Each tuple consists of a key and its corresponding value. | Syntax:
Example:
|
key existence | You can check for the existence of a key in a dictionary using the `in` keyword | Example:
|
keys() | Retrieves all keys from the dictionary and converts them into a list. Useful for iterating or processing keys using list methods. | Syntax:
Example:
|
update() | The `update()` method merges the provided dictionary into the existing dictionary, adding or updating key-value pairs. | Syntax:
Example:
|
values() | Extracts all values from the dictionary and converts them into a list. This list can be used for further processing or analysis. | Syntax:
Example:
|
Sets
Package/Method | Description | Code Example |
---|---|---|
add() | Elements can be added to a set using the `add()` method. Duplicates are automatically removed, as sets only store unique values. | Syntax:
Example:
|
clear() | The `clear()` method removes all elements from the set, resulting in an empty set. It updates the set in-place. | Syntax:
Example:
|
copy() | The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't affect the original set. | Syntax:
Example:
|
Defining Sets | A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`. They are useful for storing distinct values and performing set operations. | Example:
|
discard() | Use the `discard()` method to remove a specific element from the set. Ignores if the element is not found. | Syntax:
Example:
|
issubset() | The `issubset()` method checks if the current set is a subset of another set. It returns True if all elements of the current set are present in the other set, otherwise False. | Syntax:
Example:
|
issuperset() | The `issuperset()` method checks if the current set is a superset of another set. It returns True if all elements of the other set are present in the current set, otherwise False. | Syntax:
Example:
|
pop() | The `pop()` method removes and returns an arbitrary element from the set. It raises a `KeyError` if the set is empty. Use this method to remove elements when the order doesn't matter. | Syntax:
Example:
|
remove() | Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if the element is not found. | Syntax:
Example:
|
Set Operations | Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric difference`. | Syntax:
Example:
|
update() | The `update()` method adds elements from another iterable into the set. It maintains the uniqueness of elements. | Syntax:
Example:
|