0% found this document useful (0 votes)
154 views

Python Cheat Sheet

1) The document covers various Python concepts including escape sequences, string operators, arithmetic expressions, relational operators, while and for loops, string methods, lists, sets, and dictionaries. 2) Key list concepts include appending, removing, sorting, reversing, and slicing items. Dictionaries are created using curly brackets and values accessed via keys. 3) Control flow concepts like while and for loops are discussed alongside boolean expressions and break statements.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Python Cheat Sheet

1) The document covers various Python concepts including escape sequences, string operators, arithmetic expressions, relational operators, while and for loops, string methods, lists, sets, and dictionaries. 2) Key list concepts include appending, removing, sorting, reversing, and slicing items. Dictionaries are created using curly brackets and values accessed via keys. 3) Control flow concepts like while and for loops are discussed alongside boolean expressions and break statements.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 1 Escape Sequences Character tab double quote single quote backslash String Operators print(3 + 4) 7 print("3 + 4")

) 3 + 4 print("3"+"4") 34 print(3*4) 12 print(3*"4") 444 print("3"*4) 3333 Arithmetic Expressions ** for exponentiation 2**3 8 // for integer division the largest integer less than or equal to the value % for modulus a % b evaluates to a - (a // b)*b Chapter 2 Relational Operators Relational Operator == != > >= < <= Meaning Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to

Escape Sequence \t \" \' \\

"%.2f"%var will round variable to two decimal places, replace var with variable to be rounded Chapter 3 While: while <Boolean expression>: stmt1 stmt2 ... stmtn stmtA

What this means: 1)Evaluate the Boolean expression. 2)If its true a) Go ahead and execute stmt1 through stmtn, in order. b) Go back to step 1. 3) If the Boolean expression is false, skip over the loop and continue to stmtA. For: for <variable> in <range>: stmt1 stmt2 ... stmtn The variable is set to the first number specified in the range. Then, the statements stmt1 through stmtn are executed. Next, the variable is set to the second number in the range and the statements stmt1 through stmtn are executed again. The loop continues in this manner until the variable has been set to each number in the range, in order, and all the statements in the loop have been run each time. Chapter 4 String Commands variable.upper ALL CAPS variable.lower no caps variable.capitalize Normal Capitalization variable.isalpha alphabetic? variable.isdigit numeric? string.ascii_lowercase string.ascii_uppercase Slicing [:2] first two entries [2:] last two entries [-3:] last three entries, catalog from opposite direction str = "ABCDEFGHIJ" print(str[3:6]+"\n") will yield DEF, entries 3 to 6 excluding 3 but including 6 print(str[:7]+"\n") will yield ABCDEFG, entries before 7 including 7 print(str[5:]+"\n") will yield FGHIJ, entries after 5 print(str[-8:-5]+"\n") will yield CDE, right-to-left entries excluding -5 including -8 print(str[:-4]+"\n") will yield ABCDEF, right-to-left entries before -4

Lists Create: listname = [ ] Add: listname.append(item) Remove: listname.remove(item) Searching: item = input("What food do you want to search for?\n") for i in range(len(food)): if food[i] == item: print("Found",item,"!",sep="")

len(listname) will return the number of entries in a list listname.sort() alphabetical order open( jumbledct.txt, r) dictionary, read-mode list(variable) convert to list listname.reverse reverses the list Sets Create: newest = set ( ) Add: newset.add(item) Dictionaries Create: phonebook = {} phonebook [name] = 5551234 phonebook [name] will return 5551234 Delete: del phonebook[name] BUT WAIT THERES MORE!!! print(x & y) and, appears in both sets print(x | y) or, appears in one or the other print(x ^ y) exactly one, appears in exactly one set print(x - y) appears in x but not y print(y - x) appears in y but not x break will break a loop

You might also like