Python Summary Cheat Sheet - 2
Loops
Loops: Loops allow us to execute a block of code several times.
While Loop: Allows us to execute a block of code several times as long as the condition is True.
For Loop: for statement iterates over each item of a sequence.
Syntax:
Range: Generates a sequence of integers starting from 0. Stops before n (n is not included).
Syntax: range(n)
Range with Start and End: Generates a sequence of numbers starting from the start. Stops before the end (the end is not included).
Syntax: range(start, end)
Lists - Working with Lists
List: List is the most versatile python data structure. Holds an ordered sequence of items.
Accessing List Items: To access elements of a list, we use Indexing.
Iterating Over a List:
List Concatenation: Similar to strings, +
operator concatenates lists.
List Slicing: Obtaining a part of a list is called List Slicing.
Extended Slicing: Similar to string extended slicing, we can extract alternate items using the step.
Reversing a List: -1
for step will reverse the order of items in the list.
Slicing With Negative Index: You can also specify negative indices while slicing a List.
Negative Step Size: Negative Step determines the decrement between each index for slicing. The start index should be greater than the end index in this case
Membership check-in lists:
Name | Usage |
---|---|
in | By using the in operator, one can determine if a value is present in a sequence or not. |
not in | By using the, not in operator, one can determine if a value is not present in a sequence or not. |
Nested Lists: A list as an item of another list.
Accessing Nested List:
Accessing Items of Nested List:
List Methods
Name | Syntax | Usage | |
---|---|---|---|
append() | list.append(value) | Adds an element to the end of the list. | |
extend() | list_a.extend(list_b) | Adds all the elements of a sequence to the end of the list. | |
insert() | list.insert(index,value) | Element is inserted to the list at specified index. | |
pop() | list.pop() | Removes last element. | |
remove() | list.remove(value) | Removes the first matching element from the list. | |
clear() | list.clear() | Removes all the items from the list. | |
index() | list.index(value) | Returns the index at the first occurrence of the specified value. | |
count() | list.count(value) | Returns the number of elements with the specified value. | |
sort() | list.sort() | Sorts the list. | |
copy() | list.copy() | Returns a new list. It doesn't modify the original list. |
Functions
Functions: Block of reusable code to perform a specific action.
Defining a Function: Function is uniquely identified by the function_name
.
Calling a Function: The functional block of code is executed only when the function is called.
Function With Arguments: We can pass values to a function using an Argument.
Returning a Value: To return a value from the function use return keyword. Exits from the function when return statement is executed.
Function Arguments: A function can have more than one argument.
Keyword Arguments: Passing values by their names.
Positional Arguments: Values can be passed without using argument names. These values get assigned according to their position. Order of the arguments matters here.
Default Values:
Arbitrary Function Arguments: We can define a function to receive any number of arguments.
Variable Length Arguments: Variable length arguments are packed as tuple.
Unpacking as Arguments: If we already have the data required to pass to a function as a sequence, we can unpack it with * while passing.
Multiple Keyword Arguments: We can define a function to receive any number of keyword arguments. Variable length kwargs are packed as dictionary.
Function Call Stack: Stack is a data structure that stores items in an Last-In/First-Out manner. Function Call Stack keeps track of function calls in progress.
Recursion: A function calling itself is called a Recursion.
Passing Immutable Objects:
- Even though variable names are same, they are referring to two different objects.
- Changing the value of the variable inside the function will not affect the variable outside.
Passing Mutable Objects:
- The same object in the memory is referred by both list_a and list_x
- Default args are evaluated only once when the function is defined, not each time the function is called.
Nested Loops
Nested Loops: An inner loop within the repeating block of an outer loop is called a Nested Loop
. The Inner Loop will be executed one time for each iteration of the Outer Loop.
Syntax:
Syntax of while in for loop:
Syntax of for in while loop:
Loop Control Statements:
Name | Usage |
---|---|
Break | break statement makes the program exit a loop early. |
Continue | continue is used to skip the remaining statements in the current iteration when a condition is satisfied. |
Pass | pass statement is used as a syntactic placeholder. When it is executed, nothing happens. |
Break (In Nested Loop) | break in the inner loop stops the execution of the inner loop. |