Python Scripting - Week 4 Flashcards
What are containers in Python ?
To save multiple values in one object, containers are used
How many type of containers are in Python
There are four types of containers in Python
1. Lists
2. Tuples
3. Dictionaries
4. Sets
List in Python surrounded by
Square brackets []
How items are separated in list
Items in list are separated by comma
What type of values we put in a list
We put any type of value in list
What is indices in list
List have index positions, starts with 0 and and with total length of List
Can we slice the list
Yes we can slice the list,
What is the format of list slicing
[start,stop,step]
How to assign values to list
list1=[1,2,3,4]
list1[1]=”Hello”
list1=[i for i in range(10)]
How to insert a new value to a list ?
To insert and value to an list insert() method is used.
Index position is required to insert a value to a list
list1=[1,2,3]
list1.insert(1,20)
How to remove an item from list
We have to use remove method
list1.remove(4)
It removes first occurrence of value
list1.pop(2)
removes value at given index
What are the basic operations on list
- Assignment
- Insert
- Delete
- Append
- search
How to delete an item from last
We have to use pop method
In delete the last item permanently and also returns it
list1.pop()
How to concatenate two lists
a = [1,2,3,4]
B = [2,3,4,5]
c= a+b
Can you put list in a list
Yourwe can put list in a list
~~~
[a = [1,2,3,4]
list1+=[4,4,4,[5,5]]]
~~~
How to create a tuple
Tuples are created using brackets
mytuple = (1,2,3)
What types of values can be put in a tuple
We can put any type of values in a tuple
What kind of operations we can perform on Tuple
Tuples are immutable objects we cannot add or Delete anything inside the tuple
What are the methods of tuple ?
Index() and and count()
What is the use of index method in tuples
Index method is used to get index position of given value,
IT returns the index value of first occurrence of given value
What is the use of count method in truple
Count method is used to get, how many times a value appears in a tuple
Can we concatenate tuple
Yes we can
a = (1,2)
b = (”bob”, “mary”)
a + b
What are mutable objects
Mutable objects can change after their creation
Define mutability of list
a=[1,2,3,4]
a
b=a
a[2]=8
print(b)
print(a)
Both A & B prints same value , becoz a & b points to same location , they are immutable
What are alias of list
a=[1,2,3,4]
a
b=a
A and B alias to same memory location
What are the mutable objects in Python
Lists, dictionaries, sets
What is the basic Syntax of list comprehension ?
myList = [expression for member in iterable ]
What is iterable in Python
An object returning its members one at a time using loop.
An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop
What is boilerplate code ?
Boilerplate code, also known as boilerplate, is the part of code that has to be repeatedly in use with no or a little modification .
Simply , the same code used multiple times .
How to square a number in Python , write a program 2 square air first thousand numbers using list comprehension method ?
mylist =[i**2 for i in range(1000)]
print(mylist)
Write a program to print all even number within range, using list comprehension method ?
def even_no(num):
if num % 2 ==0:
return num
else:
return 0
mylist=[even_no(i) for i in range(100)]
print(mylist)
What is the output of this expression,
4/2
The output is 2.0
What is the output of this expression,
abs(-4/2)
2.0
What is the output of this expression,
int(abs(-4/2))
2
Write a program to create dictionary using list comprehension ?
my_dict = {i:i**2 for i in range(6)}
print(my_dict)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Write a program to create set using list comprehension ?
my_set = {i**2 for i in range(6)}
print(my_set)
{0, 1, 4, 9, 16, 25} Jack is it if we want now have a generous over but it works but it does not
Write a program to create tuple using list comprehension ?
my_tuple = tuple(i**2 for i in range(6))
print(my_tuple)
(0, 1, 4, 9, 16, 25)
Write a program to print give a number between one to hundred using comprehension method only and without function ?
my_list = [“Even No. Found” for i in range(100) if i%2==0]
print(my_list,len(my_list))
How to use “if else statement” in list comprehension method, write a program to create a list of even numbers between 1 to 100 ?
mylist=[i if i%2==0 else 100 for i in range(10)]
print(mylist)
[0, 100, 2, 100, 4, 100, 6, 100, 8, 100]
Write Program to create a matrix by using nested list comprehension ?
matrix = [[i for i in range(5)] for _ in range(6) ]
matrix
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
What is throwaway variable in nested comprehension
matrix = [[i for i in range(5)] for _ in range(6) ]
Dash is throw away variable in nested comprehension
What is the Basic Syntax of list comprehension ?
newList = [expression for member in iterable]
Which container object is immutable in python ?
tuple , you cat change value in tuple after creation.
How to Change Tuple Values ?
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
Example
x = (“apple”, “banana”, “cherry”)
y = list(x)
y[1] = “kiwi”
x = tuple(y)
print(x)