Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Handy Python CheatSheet (Python v3.7)
Atharva Shirdhankar
Atharva Shirdhankar

Posted on

     

Handy Python CheatSheet (Python v3.7)

Alt Text
Python Essential Cheatsheet📁

1. Print a string, set of strings, or object representation(s) to the console
>>> print('any string')any string>>> print('string one', 'string two')string one string two>>> print([1, 2.0, 'three', ['f','o','u','r']])[1, 2.0, 'three', ['f', 'o', 'u', 'r']]
Enter fullscreen modeExit fullscreen mode

2. Return the number of items contained within a string or object
>>> my_list = [41, 33, 48, 71, 60, 26]>>> len(my_list)6
Enter fullscreen modeExit fullscreen mode

3. Access every item and that item's index within an iterable (e.g., a list) with enumerate.
>>> countries = ['Turks & Caicos', 'Grenada', 'Vanuatu', 'Lebanon', 'Barbados']>>> for idx, item in enumerate(countries):... print(f'{idx} - {item}')...0 - Turks & Caicos1 - Grenada2 - Vanuatu3 - Lebanon4 - Barbados
Enter fullscreen modeExit fullscreen mode

4. Sort an iterable that contains objects that are all of the same type (as long as they can be compared to each other) without mutating the original object.
>>> sorted([64, 99, 69, 70, 53, 11, 42, 99, 5])[5, 11, 42, 53, 64, 69, 70, 99, 99]
Enter fullscreen modeExit fullscreen mode

5. Open any file as text for reading or writing. By default the file is opened to read. If reading, the file must actually exist at the specified absolute or relative path.
>>> with open('some_text.txt') as f:... f.read()...'Whatever text was in the file'>>> with open('writeable_file.csv', 'w') as f:... f.write('one,two,three')
Enter fullscreen modeExit fullscreen mode

6. Find out what type of object anything is with type.
>>> type([])<class 'list'>>>> type('random text')<class 'str'>
Enter fullscreen modeExit fullscreen mode

7. Check whether an object is an instance of some builtin type or user-created class with isinstance.
>>> isinstance('potato', str)True>>> isinstance('potato', list)False
Enter fullscreen modeExit fullscreen mode

8. View the documentation for any function or object that has it. To leave that view, hit the q key
>>> help(len)Help on built-in function len in module builtins:len(obj, /) Return the number of items in a container.(END)
Enter fullscreen modeExit fullscreen mode

9. Create an iterable of integers. You can either get sequential numbers starting at 0 up to (but not including) some higher number by providing one integer as an argument, set the bounds of your numbers by providing two integers, or set the bounds and the space between numbers with three integers.
>>> for number in range(4):... print(number)0123>>> for number in range(3, 33, 10):... print(number)31323
Enter fullscreen modeExit fullscreen mode

Note :
You can’t access these numbers by index like a list. You
need to loop through the range to access them.


Python in one image:
python mind map

(Image found on LinkedIn)

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

Postman Student Expert • Open Source Dev • DevOps Enthusiast
  • Location
    India
  • Joined

More fromAtharva Shirdhankar

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