Python Cheat Sheet
Python Cheat Sheet
2
1. The Empty Tuple >>>vowels[4] >>>a==b returns TRUE
T=tuple() ‘u’ >>>c=(‘2’,’3’)
2. Single Element Tuple >>>vowels[-5] >>>a==c returns FALSE
‘a’ Tuple Unpacking, tp1=(11,33,66,99)
>>>t=(1) Difference from Lists
>>>t a,b,c,d=tp1
1 L[i] =element, VALID in Lists c=77
3. Long Tuples T[i] =element, INVALID in Tuples tp1=(a,b,c,d)
>>>tp1
Sqrs=(0,1,4,9,16,25,36,49,64,81,100,
Similarity with Lists (11,33,77,99)
121,144,169,196,225)
4. Nested Tuples 1. len() Tuple Functions and Methods
T1(1,2,(3,4)) 2. Indexing and slicing
Creating Tuples from Existing 1. len() 2. max() 3. min()
3. Concatenation and Replication
Sequences Operators(+ and *)
>>>L= [‘a’,’e’,’i’,’o’,’u’] Dictionaries, {}
4. index() 5. count() 6.tuple()
4. Membership Operators
Tuple Operations Dictionaries are mutable, unordered
>>>t2=tuple (L) 5. Comparison Operators
>>>t2 1. Joining Tuples 2. Replication collections with elements in the form of a
(‘a’,’e’,’i’,’o’,’u’) >>>tp1=(1,3,4) >>>tp1*3 key: value pairs that associate keys to
Reading Tuples from the user >>>tp2=(6,7,8) (1,3,4,1,3,4,1,3,4) values
T1=tuple(input(“Enter”)) >>>tpl+tp2 Creating a Dictionary
Enter: 234567 (1, 3, 4, 6, 7, 8)
>>>T1 3. Slicing the Tuples To create a dictionary, you need to include
(‘2’,’3’,’4’,’5’,’6’,’7’) >>>tp1=(10,12,14,20,22,24,30,32) key: value pairs in curly braces as per
If more than one digit/character, use >>>seq=tp1(3:-3) following
eval(input()) (20,22) Teachers={“Dimple”:”Computer
>>>seq=[3:30] Science”,”Karen”:”Sociology”,”Harpreet”:”Math
Accessing Tuples
(20,22,24,30,32) ematics”,”Sabah”:”Legal Studies}
>>>vowels= (‘a’,’e’,’i’,’o’,’u’) 4. Comparing Tuples Key-Value Pair Key Value
>>>vowels[0] >>>a=(2,3) “Dimple”:”Computer “Dimple” “Computer
‘a’ >>>b-(2,3) Science” Science”
3
“Karen”:”Sociology” “Karen” “Sociology” >>>d.keys() >>>Employee=dict({“name”:”John”,”salary”:10000,
“Harpreet”:”Mathe “Harpreet” “Mathematics {“vowel5”,” vowel4”,” vowel3”, “age”:24}
matics” ” “vowel2”,”vowel1”} >>>Employee
“Subah”:”Legal “Subah” “Legal >>>d.values() {“salary”:10000,”age”:24,”name”:”John”}
Studies” Studies” {“a”,”e”,”i”,”o”,”u”} iii) Specify keys separately and corresponding
Accessing elements of a Dictionary \ values separately
Characteristics of a
>>>Employee=dict(zip((“name”:”salary”,”age”),
>>>teachers[“Karen”] Dictionary
(“John”,10000,24)))
Sociology 1. Unordered Set >>>Employee
>>>print(“Karen teaches”,teachers[“Karen”]) 2. Not a Sequence {“salary”:10000,”age”:24,”name”:”John”}
Karen teachers Sociology 3. Indexed by Keys, Not Numbers iv) Specify Key:value pairs separately in form of
>>>d={“vowel1”:”a”, 4. Keys must be unique sequences
“vowel2”:”e”,”vowel3”:”i”.”vowel4”:”o”,”vowel5”:” 5. Mutable >>>Employee=dict([[“name”,”John”],
u”} Working with Dictionaries
6. Internally stored as Mappings [“salary”,20000],[“age”,24]])
>>>print(d) Multiple Ways of creating Dictionaries >>>Employee
{“vowel1”:”a”,vowel2”:”e”,”vowel3”:”i”, Initializing a Dictionary {“salary”:10000,”age”:24,”name”:”John”}
”vowel4”:”o”,”vowel5”:”u”} Employee={“name”:”John”,”salary”:10000,
>>>d[“Vowel1”] ”age”:14} Adding elements in Dictionary
“a” Adding Key:value pairs to empty dictionary >>>Employee={“name”:”John”,”salary”:10000,
>>>d[“Vowel4”] i) Employees={} “age”:24}
“o” ii)Employee=dict() >>>Employee[“dept”]=”Sales”
Traversing a Dictionary Creating dictionary from name and value pairs >>>Employee
i) Specific Key:value pairs as keyword arguments to {“salary”:10000,”dept”:”Sales”,”age”:24,”name”:”J
for key in d: dict() ohn”}
print(key,”:”,d1[key]) Employee=dict(name=”John”,salary=10000,
age=24} Updating existing elements in Dictionary
Accessing Keys or >>>Employee >>> Employee={“name”:”John”,”salary”:10000,
Values Simultaneously {“salary”:10000,”age”:24,”name”:”John”} “age”:24}
ii) Specific comma-separated key:value pairs >>>Employee[“salary”]=20000
4
>>>Employee 8. split()
{“salary”:20000,”age”:24,”name”:”John} 9. dumps()[import JSON]
>>>empl3
{“salary”:10000,”age”:24,”name”:”John”}
>>>del.empl3[“age”]
>>>empl3
{“salary:10000,”name”:”John”}
>>>empl3.pop(“name”)
“John”
>>>empl3
{“salary”:10000}
>>>”age” in empl3
False
>>>”salary” in empl3
True
1. len()
2. clear()
3. get()
4. items()
5. keys()
6. values()
7. update()
5