Py Functions Flashcards

1
Q

string function, how to break a longer string into smaller strings?

A

]] < long_string >.split( ‘ )

    • split
    • it will split the long_string by the split_char into
    • strings which the long_string was made up of
    • built-in string function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to remove unwanted characters from the beginning or end of a string?

A

]] < short_string >.strip( char_to_remove )

    • strip
    • strip away the char_remove from the beginning or
    • end of the short_string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how can you find out if a value is part of a list or not? , a function also used in SQL
( provide a practical example )

A

]] < value > in < list_name >
]] True / False
– in
– query the list to get the response whether a value is included (True) or not (False)

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

how to ask whether a value/string is numeric, or not?
what will it check?
what will the answer reveal?
which other function is closely related?

A

]] < string >.isnumeric( )

– it checks whether all characters in the value are numeric, incl expressions as power of etc.
– it should probably be considered as a check of chars and expression
– provided answer is ‘True’ or ‘False’
– another closely related function is .isdigit()
– difference between the two are the latter considering whether all chars incl in string are digits or not, e.g. ‘,.’ will give False
—| APPLICATION |—
go through individual chars in a for-loop
—| EXAMPLES |—
‘1234567890’.isdigit()
Out[2]: True
‘1234567890’.isdigit()
Out[2]: True
‘1234567890’.isnumeric()
Out[4]: True
‘1234.567890’.isnumeric()
Out[5]: False

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

how to ask whether a value is alphanumeric?

A

]] < string >.isalpha( )
]] < string >.isalnum( )
]] < string >.isascii( )

    • all similar in use
    • I find the latest to be the most clarifying
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to add different strings or chars together?

is it a built-in function?

A

1 - join
2 - no, it does not come up under built-in functions

]] .join( string/list/… )

e.g. from geeks for geeks
]] list1 = [ ‘1’,’2’,’3’,’4’ ]
]] s = ‘-‘
]] str_together = s.join( list1 )

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

how to apply the same function to iterable values?

is it a built-in function?

A

1 - map
2 - yes, built-in function

]] result = map( fn, iteration )

# geeksforgeeks
# Python program to demonstrate working of map. 
# Return double of n 
def addition( n ): 
	return n + n 
# We double all numbers using map() 
numbers = ( 1, 2, 3, 4) 
result      = map( addition, numbers) 
print( list( result))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to find the char-number in the ascii charset?
reversely, how to the find the char from the ascii char-number?
are these built-in functions?

A

1 - ord()
]] ord( ‘a’ ) => 97
]] chr( 97 ) => a

– yes, these are built-in functions

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

how to express/move chars from one charset to another?
is this a function?
how is it applied?

A

1 - decode
2 - no, this is not a function, it’s a method

# initializing string 
str = "geeksforgeeks"
# encoding string 
str_enc = str.encode( 'base64', 'strict' ) 
# printing the encoded string 
print "The encoded string in base64 format is : ", 
print str_enc 
# printing the original decoded string 
print "The decoded string is : ", 
print str_enc.decode( 'base64', 'strict' ) 

The encoded string in base64 format is : Z2Vla3Nmb3JnZWVrcw==

The decoded string is : geeksforgeeks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
how to list all functions for a module or class?
is this built-in?
A

1 - dir
2 - yes, this is a built-in function

]] dir( )

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

how to divide in Python?

A
//
]] def div_10( upper_value )
]]     return upper_value // 10
]] print( div_10( 120 ) )
]]  12
How well did you know this?
1
Not at all
2
3
4
5
Perfectly