Dictionaries
The dictionary is an unordered set of comma-separated key:value pairs, within {}, with the requirement that within a dictionary, no two keys can be the same.
Dictionary
<dictionary-name> = {<key>: value, <key>: value ...}
Adding Element to a dictionary
By this method, one can add new elements to the dictionary
<dictionary>[<key>] = <value>
Updating Element in a dictionary
If a specified key already exists, then its value will get updated
<dictionary>[<key>] = <value>
Deleting an element from a dictionary
del keyword is used to delete a specified key:value pair from the dictionary as follows:
del <dictionary>[<key>]
Dictionary Functions & Methods
Below are some of the methods of dictionaries
len() method
It returns the length of the dictionary, i.e., the count of elements (key: value pairs) in the dictionary
len(dictionary)
clear() method
Removes all the elements from the dictionary
dictionary.clear()
**get() method
**Returns the value of the specified key
dictionary.get(keyname)
items() method
Returns a list containing a tuple for each key-value pair
dictionary.items()
keys() method
Returns a list containing the dictionary's keys
dictionary.keys()
values() method
Returns a list of all the values in the dictionary
dictionary.values()
update() method
Updates the dictionary with the specified key-value pairs
dictionary.update(iterable)
Conditional Statements
The if, elif and else statements are the conditional statements in Python, and these implement selection constructs (decision constructs).
if Statement
if(conditional expression):
statements
if-else Statement
if(conditional expression):
statements
else:
statements
if-elif Statement
if (conditional expression):
statements
elif (conditional expression):
statements
else:
statements
Nested if-else Statement
if (conditional expression):
if (conditional expression):
statements
else:
statements
else:
statements
Loops in Python
A loop or iteration statement repeatedly executes a statement, known as the loop body, until the controlling expression is false (0).
For Loop
The for loop of Python is designed to process the items of any sequence, such as a list or a string, one by one.
for <variable> in <sequence>:
statements_to_repeat
While Loop
A while loop is a conditional loop that will repeat the instructions within itself as long as a conditional remains true.
while <logical-expression>:
loop-body
Break Statement
The break statement enables a program to skip over a part of the code. A break statement terminates the very loop it lies within.
for <var> in <sequence>:
statement1
if <condition>:
break
statement2
statement_after_loop
Continue Statement
The continue statement skips the rest of the loop statements and causes the next iteration to occur.
for <var> in <sequence>:
statement1
if <condition> :
continue
statement2
statement3
statement4
Functions
A function is a block of code that performs a specific task. You can pass parameters into a function. It helps us to make our code more organized and manageable.
Function Definition
def my_function(parameters):
pass #statements
Top comments (0)