Py Functions Flashcards
string function, how to break a longer string into smaller strings?
]] < 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 to remove unwanted characters from the beginning or end of a string?
]] < short_string >.strip( char_to_remove )
- strip
- strip away the char_remove from the beginning or
- end of the short_string
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 )
]] < value > in < list_name >
]] True / False
– in
– query the list to get the response whether a value is included (True) or not (False)
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?
]] < 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 to ask whether a value is alphanumeric?
]] < string >.isalpha( )
]] < string >.isalnum( )
]] < string >.isascii( )
- all similar in use
- I find the latest to be the most clarifying
how to add different strings or chars together?
is it a built-in function?
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 to apply the same function to iterable values?
is it a built-in function?
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 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?
1 - ord()
]] ord( ‘a’ ) => 97
]] chr( 97 ) => a
– yes, these are built-in functions
how to express/move chars from one charset to another?
is this a function?
how is it applied?
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 to list all functions for a module or class? is this built-in?
1 - dir
2 - yes, this is a built-in function
]] dir( )
how to divide in Python?
// ]] def div_10( upper_value ) ]] return upper_value // 10 ]] print( div_10( 120 ) ) ]] 12