Srinivas Ram Cheat Sheet for Python Topics
Srinivas Ram Cheat Sheet for Python Topics
myList = list(range(5)) # a list with five items the list ['kiwi', 'watermelon'] to the end of the my_list = [[1, 2], [3, 4]]
[0, 1, 2, 3, 4] list. print(my_list[0][1]) # Output: 2
myList = [i*2 for i in range(5)] a list with five my_list.remove('cherry') removes the
List Functions:
items [0, 2, 4, 6, 8] element 'cherry' from the list.
sum(my_list)
my_list.pop(0) removes and returns the first
Accessing Elements : all(my_list)
element in the list ('apple').
Elements in a list can be accessed using any(my_list)
List Methods :
indexing or slicing. enumerate(my_list)
Lists have many built-in methods, including: zip(my_list1, my_list2)
# Accessing an element using indexing :
# Using append() method : List Comprehensions :
my_list = [1, 2, 3, 4, 5]
Print(my_list[0]) # output: 1 accesses the my_list = [1, 2, 3] List comprehensions provide a concise way
first element in the list (1). my_list.append(4) to create lists based on existing lists.
print(my_list)
# Accessing a slice of elements using # Creating a new list using list compre‐
# Output: [1, 2, 3, 4]
slicing : hension :
# Using extend() method :
print(my_list[1:4])) # Output: [2, 3, 4] my_list = [1, 2, 3, 4, 5]
my_list.extend([5, 6]) new_list = [x * 2 for x in my_list]
Some examples on indexing and slicing :
print(my_list) print(new_list) # Output: [2, 4, 6, 8, 10]
my_list = ['apple', 'banana', 'cherry']
# Output: [1, 2, 3, 4, 5, 6] my_list = [x for x in range(1, 6)]
print(my_list[1]) # Output: banana accesses
# Using insert() method : even_list = [x for x in range(1, 11) if x % 2
the second element in the list ('banana').
== 0]
print(my_list[-1]) # Output: cherry accesses my_list.insert(0, 0)
the last element in the list ('cherry'). print(my_list) Advantage of Using Lists :
print(my_list[1:]) # Output: ['banana', # Output: [0, 1, 2, 3, 4, 5, 6] Lists are mutable, which makes them more
'cherry'] accesses all elements in the list # Using remove() method : flexible to use than tuples.
from the second element to the end
my_list.remove(3)
Modifying Elements : print(my_list)
# Output: [0, 1, 2, 4, 5, 6]
# Using pop() method :
my_list.pop(2)
print(my_list)
# Output: [0, 1, 4, 5, 6]
What are Tuples? Returns the number of times a specified # Accessing a value by key
A tuple is an ordered, immutable collection value occurs in a tuple. my_dict["key1"] # returns "value1"
of elements. my_tuple = (1, 2, 2, 3, 2, 4) # Using the get() method to avoid KeyError
In Python, tuples are created using parent‐ # Count the number of times the value 2 my_dict.get("key1") # returns "value1"
heses () appears my_dict.get("key4") # returns None
and the elements are separated by commas print(my_tuple.count(2)) # Output: 3 # Using the get() method with a default
,. index() value
my_dict.get("key4", "default_value") #
Creating Tuples Returns the index of the first occurrence of a
returns "default_value"
# Create an empty tuple specified value in a tuple.
my_tuple = (1, 2, 2, 3, 2, 4) Adding and updating values
my_tuple = ()
# Create a tuple with elements # Find the index of the first occurrence of # Adding a new key-value pair