# Built-in Functions (1) Flashcards

1
Q
```#Built in functions
#Write the arguments of slice()```
A

slice(stop)

slice(start, stop, step)

2
Q

Outcome of:

String =’GeeksforGeeks’

Gee
ek

A

s1 = slice(3)
s2 = slice(1, 5, 2)
print(String[s1])
print(String[s2])

3
Q
```#Built in functions
#Write the arguments of .countOf()```
A

Operator.countOf(freq = a, value = b)

4
Q

Count frequency of 3

arr = [ 1, 2, 3, 3, 3 ]

A

from operator import *
arr = [ 1, 2, 3, 3, 3 ]
print(countOf(arr, 3))

5
Q
```#Built in functions
#Write the arguments of copysign() and explain what it does```
A

math.copysign(x, y)

Returns : float value consisting of magnitude from parameter x and the sign from parameter y.

6
Q

Outcome of:

import math
math.copysign(-5,7)

A

5

7
Q
```#Built in functions
# int.bit_length(), explain what it does```
A

Returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros.

8
Q

Outcome of:

num = 7
print(num.bit_length())

A

3

9
Q
```#Built in functions
#Write the arguments of isinstance() and explain what it does```
A

isinstance(obj, class)

Returns : True, if object belongs to the given class/type if single class is passed or any of the class/type if tuple of class/type is passed, else returns False. Raises a TypeError if anything other than mentioned valid class type.

10
Q

Check whether the object test=’stre’ belongs to the class integer

A

test=’stre’
isinstance(test, int)

Outcome:
False

11
Q

Write the arguments of range()

A

range(start, stop, step)

12
Q

Get this outcome using range():

[0, 2, 4, 6, 8]

A

list(range(0,10,2))

13
Q

Write the arguments of set() and explain what it does

A

set(iterable)

Returns : An empty set if no element is passed. Non-repeating element iterable modified as passed as argument.

14
Q

Code

lis1 = [ 3, 4, 1, 4, 5 ]

{1, 3, 4, 5}

A

set(lis1)

15
Q

Parameters of hash(obj) and explain what it does

A

obj : The object which we need to convert into hash.

Returns : Returns the hashed value if possible.

16
Q

Outcome of:

lis1 = [1, 2, 3, 4, 5]
lis1 = iter(lis1)
next(lis1)

A

Once the iteration is complete, it raises a StopIteration exception and iteration count cannot be
reassigned to 0.
herefore, it can be used to traverse the container just once.

17
Q

Arguments of iter() and explain limitations

A

iter(obj, sentinel)

obj : Object which has to be converted to iterable ( usually an iterator ).
sentinel : value used to represent end of sequence.

Once the iteration is complete, it raises a StopIteration exception and iteration count cannot be
reassigned to 0.

18
Q

Complete code

```class GfG :
name = "GeeksforGeeks"
age = 24```

…= GfG

…(obj,’name’)

Outcome:
‘GeeksforGeeks’

A
```class GfG :
name = "GeeksforGeeks"
age = 24```

obj = GfG

getattr(obj,’name’)

19
Q

Explain random.sample() function

A

Syntax : random.sample(sequence, k)
Parameters:
sequence: Can be a list, tuple, string, or set.
k: An Integer value, it specify the length of a sample.
random sampling without replacement.

20
Q

Get a sample of three numbers from a list called list1

A

from random import sample
list1 = [1, 2, 3, 4, 5]
sample(list1,3)

Outcome:
[2, 3, 4]

21
Q

Finish code

from collections import …

x = …(“geeksforgeeks”)

Outcome:

g g e e e e k k s s f o r

A

from collections import Counter
x = Counter(“geeksforgeeks”)
for i in x.elements():
print ( i, end = “ “)

22
Q

Code to get:

bytearray(b’Geeksforgeeks’)

A

str = “Geeksforgeeks”
array1 = bytearray(str, ‘utf-8’)
print(array1)

23
Q

standard deviation of list1.

A

import statistics

```# creating a simple data - set
list1 = [1, 2, 3, 4, 5]```
24
Q

Get the standard deviation

list1=list(range(1,8))

```#Outcome:
2.16```
A

round(statistics.stdev(list1),2)

25
Q

Calculate the median of :

list1=np.linspace(0,2,11)

A

import statistics
import numpy as np
list1=np.linspace(0,2,11)
statistics.median(list1)

Outcome:
1.0

26
Q

Get the mode

data1 = (2, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 7)

A

from statistics import mode
mode(data1)

Outcome:
5

27
Q

Print each letter in the same line with one second of difference

strn = “GeeksforGeeks”

A

import time
for i in range(0, len(strn)):
print(strn[i], end =””)
time.sleep(2)

28
Q

Get the complete current time

e.g. 2020-04-25 14:47:50.957890

A

current_time = datetime.datetime.now()

print(current_time)

29
Q

Complete the code

print (…(“Python”))

```#Outcome:
A

print (str.format(“Python”))

30
Q
```#Complete the code
….
# Function which generates a new
# random number everytime it executes
def generator():
...(1, 10)```
A

from random import randint

```def generator():
return randint(1, 10)```
31
Q

Complete the code

a = {‘x’:’John’, ‘y’:’Wick’}

```#Outcome:
John's last name is Wick```
A

print(“{x}’s last name is {y}”.format_map(a))

32
Q

Split the word using @ as separator only once

word = ‘geeks@for@geeks’

```#Outcome:
['geeks@for', 'geeks']```
A

print(word.rsplit(‘@’, 1))

33
Q

Remove ‘b’

list2 = [ ‘a’, ‘b’, ‘c’, ‘d’ ]

```#Outcome:
['a', 'c', 'd']```
A

list2.remove(‘b’)

print(list2)

34
Q

Code

string = ‘ Geeks for Geeks ‘

```#Outcome:
print(string.strip())```
A

string = ‘ Geeks for Geeks ‘

```# Leading spaces are removed
print(string.strip())```
35
Q

Outcome of :

string = ‘ Geeks for Geeks ‘
print(string.strip(‘Geeks’))

A

Geeks for Geeks

```#To remove Geeks should be:
print(string.strip(' Geeks'))
#The function returns another string with both leading and trailing characters being stripped off.```
36
Q

Get the factor of this list

seq = [0, 1, 2, 3, 5, 8, 13]

```#Outcome:
[0, 2, 8]```
A

result = filter(lambda x: x % 2 == 0, seq)

print(list(result))

37
Q

Get all the permutations for:

s = “GEEK”

```#Outcome
GEEK
GEKE
GKEE
EGEK
EGKE
EEGK
EEKG
EKGE
EKEG
KGEE
KEGE
KEEG```
A

from itertools import permutations

```s = "GEEK"
p = permutations(s)```
```d = []
for i in list(p):
if (i not in d):
d.append(i)
print(''.join(i))```