Essential Python Cheat Sheet by Sschaub, Cheat Sheet of Information Technology

Essential Python Cheat Sheet for Beginners

Typology: Cheat Sheet

2018/2019

Uploaded on 06/20/2019

ali-abdelhalim
ali-abdelhalim 🇪🇬

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf2

Partial preview of the text

Download Essential Python Cheat Sheet by Sschaub and more Cheat Sheet Information Technology in PDF only on Docsity!

Cheatography Integer -256, 15 Float -253.23, 1.253e-10 String "Hello", ‘Goodbye’, ""Multiine"™" Boolean True, False List [value, ...] Tuple (value, ...)1 Dictionary _{ key: value, ...} Set { value, value, ... }2 1 Parentheses usually optional 2 Create an empty set with set() If Statement if expression: statements elif expression: statements else: statements While Loop while expression: statements For Loop for var in collection: statements Counting For Loop for i in range(start, end [, step): statements (start is included; end is not) Arithmetic Operators x+y add x-y subtract xty multiply x/y divide x%y modulus xy x7 Assignment shortcuts: x op= y Example: x += 1 increments x By sschaub cheatography.com/sschaub/ Essential Python Cheat Sheet by sschaub via cheatography.com/1000/cs/374/ ee uae eer xy Greater. x>=y Greater or eq y Equal xl=y — Notequal EP eeu not x xandy xory te aU] try: ‘statements except [ exception type [ asvar ] ]: ‘statements finally: ‘statements Ce ed int(exph Converts expr to integer float(expr) Converts expr to float str(expr) Converts expr to string chr(num) ASCII char num len(s) length of s sii] ith item ins (0-based) s{start: slice of s from start (included) to end] end (excluded) xins Trueifx is contained ins xnotins True ifx is not contained ins s+t the concatenation of s with t s'n ncopies of s concatenated sorted(s a sorted copy of s ) s.index( position ins of item item) Deesan Reser) s.lower() lowercase copy of s s.replace(old, copy of s with old replaced new) with new s.split( delim) list of substrings delimited by delim Published 21st May, 2012. Last updated 2nd June, 2014. Page 1 of 2. TSU Re) eet nek Cn) s.strip() copy of s with whitespace trimmed s.upper() uppercase copy of s See also http://docs.python.orgfibrary/stdtypes. htmifstring- methods TET eee) eed del Ist] Deletes ith item from Ist Ist.append(e) Appends eto Ist Ist.insert(,e) Inserts e before ith item in Ist Ist.sort() Sorts ist See also http://docs.python.orgfibrary/stdtypes. htmitttypess eq-mutable Dee Say len(d) Number of items ind del key] Removes key from d keyind True ifd contains key dkeys() Returns a list of keys ind See also http://docs. python.org/ibrary/stdtypes. html#mappi ng-types-dict Function Det Try def name(arg!, arg2, statements return expr sys.argv _List of command line arguments (argv{0] is executable) os.environ Dictionary of environment variables os.curdir String with path of current directory import sys; print(sys.argv) or from sys import argy; print(argv) Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords! http://crosswordcheats.com