Built-in Functions (1) Flashcards

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

slice(stop)

slice(start, stop, step)

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

Outcome of:

String =’GeeksforGeeks’

Gee
ek

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
#Built in functions
#Write the arguments of .countOf()
A

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

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

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

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

Outcome of:

import math
math.copysign(-5,7)

A

5

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

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

Outcome of:

num = 7
print(num.bit_length())

A

3

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

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

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

A

test=’stre’
isinstance(test, int)

Outcome:
False

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

Write the arguments of range()

A

range(start, stop, step)

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

Get this outcome using range():

[0, 2, 4, 6, 8]

A

list(range(0,10,2))

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

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

Code

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

{1, 3, 4, 5}

A

set(lis1)

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
#Calculate the median of : list1=np.linspace(0,2,11)
import statistics import numpy as np list1=np.linspace(0,2,11) statistics.median(list1) Outcome: 1.0
26
#Get the mode data1 = (2, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 7)
from statistics import mode mode(data1) Outcome: 5
27
#Print each letter in the same line with one second of difference strn = "GeeksforGeeks"
import time for i in range(0, len(strn)): print(strn[i], end ="") time.sleep(2)
28
#Get the complete current time e.g. 2020-04-25 14:47:50.957890
current_time = datetime.datetime.now() | print(current_time)
29
#Complete the code str = "This article is written ..." print (...("Python")) ``` #Outcome: This article is written in Python ```
str = "This article is written in {}" | print (str.format("Python"))
30
``` #Complete the code …. # Function which generates a new # random number everytime it executes def generator(): ...(1, 10) ```
from random import randint ``` def generator(): return randint(1, 10) ```
31
#Complete the code a = {'x':'John', 'y':'Wick'} … ``` #Outcome: John's last name is Wick ```
print("{x}'s last name is {y}".format_map(a))
32
#Split the word using @ as separator only once word = 'geeks@for@geeks' ``` #Outcome: ['geeks@for', 'geeks'] ```
print(word.rsplit('@', 1))
33
#Remove ‘b’ list2 = [ 'a', 'b', 'c', 'd' ] ``` #Outcome: ['a', 'c', 'd'] ```
list2.remove('b') | print(list2)
34
#Code string = ' Geeks for Geeks ' ``` #Outcome: print(string.strip()) ```
string = ' Geeks for Geeks ' ``` # Leading spaces are removed print(string.strip()) ```
35
#Outcome of : string = ' Geeks for Geeks ' print(string.strip('Geeks'))
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
#Get the factor of this list seq = [0, 1, 2, 3, 5, 8, 13] ``` #Outcome: [0, 2, 8] ```
result = filter(lambda x: x % 2 == 0, seq) | print(list(result))
37
#Get all the permutations for: s = "GEEK" ``` #Outcome GEEK GEKE GKEE EGEK EGKE EEGK EEKG EKGE EKEG KGEE KEGE KEEG ```
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)) ```