Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 modeExit fullscreen mode

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

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

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

<dictionary>[<key>] = <value>
Enter fullscreen modeExit 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 modeExit 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 modeExit fullscreen mode

clear() method
Removes all the elements from the dictionary

dictionary.clear()
Enter fullscreen modeExit fullscreen mode

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

dictionary.get(keyname)
Enter fullscreen modeExit fullscreen mode

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

dictionary.items()
Enter fullscreen modeExit fullscreen mode

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

dictionary.keys()
Enter fullscreen modeExit fullscreen mode

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

dictionary.values()
Enter fullscreen modeExit fullscreen mode

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

dictionary.update(iterable)
Enter fullscreen modeExit 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 modeExit fullscreen mode

if-else Statement

if(conditional expression):    statementselse:    statements
Enter fullscreen modeExit fullscreen mode

if-elif Statement

if (conditional expression):    statementselif (conditional expression):    statementselse:    statements
Enter fullscreen modeExit fullscreen mode

Nested if-else Statement

if (conditional expression):    if (conditional expression):        statements    else:        statementselse:    statements
Enter fullscreen modeExit 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 modeExit 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 modeExit 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    statement2statement_after_loop
Enter fullscreen modeExit 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 modeExit 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 Definitiondef my_function(parameters):    pass #statements
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer 🧑‍🎓 Passionate about Web Development, Python Development and open-source. Book lover 📔 || Music Lover 🎶 || 💻 Internet surfer 🏄
  • Location
    Hyderabad, India
  • Education
    Master's in Computer Applications
  • Work
    Data Engineer
  • Joined

More fromSandeep

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp