Python Scripting - Week 4 Flashcards

1
Q

What are containers in Python ?

A

To save multiple values in one object, containers are used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How many type of containers are in Python

A

There are four types of containers in Python
1. Lists
2. Tuples
3. Dictionaries
4. Sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List in Python surrounded by

A

Square brackets []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How items are separated in list

A

Items in list are separated by comma

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of values we put in a list

A

We put any type of value in list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is indices in list

A

List have index positions, starts with 0 and and with total length of List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can we slice the list

A

Yes we can slice the list,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the format of list slicing

A

[start,stop,step]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to assign values to list

A

list1=[1,2,3,4]
list1[1]=”Hello”
list1=[i for i in range(10)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to insert a new value to a list ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to remove an item from list

A

We have to use remove method
list1.remove(4)
It removes first occurrence of value
list1.pop(2)
removes value at given index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the basic operations on list

A
  1. Assignment
  2. Insert
  3. Delete
  4. Append
  5. search
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to delete an item from last

A

We have to use pop method
In delete the last item permanently and also returns it
list1.pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to concatenate two lists

A

a = [1,2,3,4]
B = [2,3,4,5]
c= a+b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you put list in a list

A

Yourwe can put list in a list
~~~
[a = [1,2,3,4]
list1+=[4,4,4,[5,5]]]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to create a tuple

A

Tuples are created using brackets
mytuple = (1,2,3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What types of values can be put in a tuple

A

We can put any type of values in a tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What kind of operations we can perform on Tuple

A

Tuples are immutable objects we cannot add or Delete anything inside the tuple

19
Q

What are the methods of tuple ?

A

Index() and and count()

20
Q

What is the use of index method in tuples

A

Index method is used to get index position of given value,
IT returns the index value of first occurrence of given value

21
Q

What is the use of count method in truple

A

Count method is used to get, how many times a value appears in a tuple

22
Q

Can we concatenate tuple

A

Yes we can
a = (1,2)
b = (”bob”, “mary”)
a + b

23
Q

What are mutable objects

A

Mutable objects can change after their creation

24
Q

Define mutability of list

A

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

25
Q

What are alias of list

A

a=[1,2,3,4]
a
b=a
A and B alias to same memory location

26
Q

What are the mutable objects in Python

A

Lists, dictionaries, sets

27
Q

What is the basic Syntax of list comprehension ?

A

myList = [expression for member in iterable ]

28
Q

What is iterable in Python

A

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

29
Q

What is boilerplate code ?

A

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 .

30
Q

How to square a number in Python , write a program 2 square air first thousand numbers using list comprehension method ?

A

mylist =[i**2 for i in range(1000)]
print(mylist)

31
Q

Write a program to print all even number within range, using list comprehension method ?

A

def even_no(num):
if num % 2 ==0:
return num
else:
return 0
mylist=[even_no(i) for i in range(100)]
print(mylist)

32
Q

What is the output of this expression,
4/2

A

The output is 2.0

33
Q

What is the output of this expression,
abs(-4/2)

A

2.0

34
Q

What is the output of this expression,
int(abs(-4/2))

A

2

35
Q

Write a program to create dictionary using list comprehension ?

A

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}

36
Q

Write a program to create set using list comprehension ?

A

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

37
Q

Write a program to create tuple using list comprehension ?

A

my_tuple = tuple(i**2 for i in range(6))
print(my_tuple)
(0, 1, 4, 9, 16, 25)

38
Q

Write a program to print give a number between one to hundred using comprehension method only and without function ?

A

my_list = [“Even No. Found” for i in range(100) if i%2==0]
print(my_list,len(my_list))

39
Q

How to use “if else statement” in list comprehension method, write a program to create a list of even numbers between 1 to 100 ?

A

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]

40
Q

Write Program to create a matrix by using nested list comprehension ?

A

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]]

41
Q

What is throwaway variable in nested comprehension

A

matrix = [[i for i in range(5)] for _ in range(6) ]
Dash is throw away variable in nested comprehension

42
Q

What is the Basic Syntax of list comprehension ?

A

newList = [expression for member in iterable]

43
Q

Which container object is immutable in python ?

A

tuple , you cat change value in tuple after creation.

44
Q

How to Change Tuple Values ?

A

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)