DEV Community

Sandeep
Sandeep

Posted on

Python Cheat Sheet Part - 3

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 ...}
Enter fullscreen mode Exit fullscreen mode

Adding Element to a dictionary
By this method, one can add new elements to the dictionary

<dictionary>[<key>] = <value>
Enter fullscreen mode Exit fullscreen mode

Updating Element in a dictionary
If a specified key already exists, then its value will get updated

<dictionary>[<key>] = <value>
Enter fullscreen mode Exit fullscreen mode

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>]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

clear() method
Removes all the elements from the dictionary

dictionary.clear()
Enter fullscreen mode Exit fullscreen mode

**get() method
**Returns the value of the specified key

dictionary.get(keyname)
Enter fullscreen mode Exit fullscreen mode

items() method
Returns a list containing a tuple for each key-value pair

dictionary.items()
Enter fullscreen mode Exit fullscreen mode

keys() method
Returns a list containing the dictionary's keys

dictionary.keys()
Enter fullscreen mode Exit fullscreen mode

values() method
Returns a list of all the values in the dictionary

dictionary.values()
Enter fullscreen mode Exit fullscreen mode

update() method
Updates the dictionary with the specified key-value pairs

dictionary.update(iterable)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

if-else Statement

if(conditional expression):
    statements
else:
    statements
Enter fullscreen mode Exit fullscreen mode

if-elif Statement

if (conditional expression):
    statements
elif (conditional expression):
    statements
else:
    statements
Enter fullscreen mode Exit fullscreen mode

Nested if-else Statement

if (conditional expression):
    if (conditional expression):
        statements
    else:
        statements
else:
    statements
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Python cheat sheet part-1

Python cheat sheet part-2

Top Websites to learn Python

How to install python in windows 10

Best IDE's for Python

Top comments (0)