math.acos() Returns the arc cosine of a number
math.acosh() Returns the inverse hyperbolic cosine of a number
math.asin() Returns the arc sine of a number
math.asinh() Returns the inverse hyperbolic sine of a number
math.atan() Returns the arc tangent of a number in radians
math.atan2() Returns the arc tangent of y/x in radians
math.atanh() Returns the inverse hyperbolic tangent of a number
math.ceil() Rounds a number up to the nearest integer
math.comb() Returns the number of ways to choose k items from n items without repetition and order
math.copysign() Returns a float consisting of the value of the first parameter and the sign of the second parameter
math.cos() Returns the cosine of a number
math.cosh() Returns the hyperbolic cosine of a number
math.degrees() Converts an angle from radians to degrees
math.dist() Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point
math.erf() Returns the error function of a number
math.erfc() Returns the complementary error function of a number
math.exp() Returns E raised to the power of x
math.expm1() Returns Ex -1
math.fabs() Returns the absolute value of a number
math.factorial() Returns the factorial of a number
math.floor() Rounds a number down to the nearest integer
math.fmod() Returns the remainder of x/y
math.frexp() Returns the mantissa and the exponent, of a specified number
math.fsum() Returns the sum of all items inany iterable (tuples, arrays, lists, etc.)
math.gamma() Returns the gamma function at x
math.gcd() Returns the greatest common divisor of two integers
math.hypot() Returns the Euclidean norm
math.isclose() Checks whether two values are close to each other,ornot
math.isfinite() Checks whether a number is finite ornot
math.isinf() Checks whether a number is infinite ornot
math.isnan() Checks whether a value is NaN (not a number)ornot
math.isqrt() Rounds a square root number downwards to the nearest integer
math.ldexp() Returns the inverse of math.frexp() which is x *(2**i) of the given numbers x and i
math.lgamma() Returns the log gamma value of x
math.log() Returns the natural logarithm of a number,or the logarithm of number to base
math.log10() Returns the base-10 logarithm of x
math.log1p() Returns the natural logarithm of 1+x
math.log2() Returns the base-2 logarithm of x
math.perm() Returns the number of ways to choose k items from n items with order and without repetition
math.pow() Returns the value of x to the power of y
math.prod() Returns the product of all the elements in an iterable
math.radians() Converts a degree value into radians
math.remainder() Returns the closest value that can make numerator completely divisible by the denominator
math.sin() Returns the sine of a number
math.sinh() Returns the hyperbolic sine of a number
math.sqrt() Returns the square root of a number
math.tan() Returns the tangent of a number
math.tanh() Returns the hyperbolic tangent of a number
math.trunc() Returns the truncated integer parts of a number
Arithmetic:
operator | name | example |+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
Comparison:
Operator | Name | Example |== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Assignment:
Operator | Example |= x =5+= x +=3-= x -=3*= x *=3/= x /=3%= x %=3//= x //=3**= x **=3&= x &=3|= x |=3^= x ^=3>>= x >>=3<<= x <<=3
txt ="this is a wild string"print(txt.replace("i","x"))# print string with all i characters replaced with xprint(txt.replace("i","x",2))# print string with first two i characters found with xprint(txt.upper())# print string in all uppercase lettersprint(txt.lower())# print string in all uppercase lettersprint(ord('A'))# print the ordinal value of a characterprint(chr(95))# print character from its ordinal valueprint('Yes'*5)# print string Yes 5 times# Reference strings by indexprint(txt[0])# print first letter of string from starting indexprint(txt[0:2])# print first two letters from starting indexprint(txt[1:])# print all characters except the first letterprint(txt[0::2])# print every second characterprint(txt[::-1])# print string in reverseprint(txt[-1])# print the last character in a stringprint(txt[-2:])# print the last who characters in a string# check if a wild is found in txtif"wild"in txt:print("wild is found in txt")# check if a blah is not found in txtif"blah"notin txt:print("is not found in txt")# Check if txt starts with thisif txt.startswith("this"):print("Starts with this")# check if txt ends with ingif txt.endswith("ing"):print("Ends with ing")# Split a string into a tuple when the delimiter is first encountered
txt ='random-data'
data_split = txt.partition('-')print(data_split)# output ('random', '-', 'data')len(txt)# Return length of string# loop through each character in stringfor char in txt:print(char)# Display price with commas and 2 digit precision
price =9749000
display_price =f"My price {price:,.2f}"print(display_price)
fruits =['orange','apple','pear','banana','kiwi','apple','banana']
fruits.count('apple')# count number of apples found in list# output 2
fruits.count('tangerine')# count number of tangerines in list# output 0
fruits.index('banana')# find the first index of banana# output 3
fruits.index('banana',4)# Find next banana starting a position 4# output 6
fruits.reverse()# reverse fruits array
fruits
# output ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
fruits.append('grape')# append grape at the end of array
fruits
# output ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
fruits.sort()
fruits
# output ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']len(fruits)# length of fruits array# output 8# loop and print each fruitfor fruit in fruits:print(fruit)
empty_set =set()
basket ={'apple','orange','apple','pear','orange','banana'}print(basket)# show that duplicates have been removed# output {'orange', 'banana', 'pear', 'apple'}# check if orange is in basket setprint('orange'in basket)# output true# convert a string to a set of letters - sets contains no duplicates
set_a =set('abcd')
set_b =set('bcde')# the operations below returns new sets# print letters in set_a but not in set_b - differenceprint(set_a - set_b)# output {'a'}# print set letters that is in either set a or b - unionprint(set_a | set_b)# output {'a', 'c', 'e', 'b', 'd'}# print letters that are in both set_a and set_b - intersectionprint(set_a & set_b)# output {'c', 'd', 'b'}# print letters that are in set_a and set_b when the letters are found in a set but no the other set - symmetric_difference()print(set_a ^ set_b)# output {'a', 'e'}# Creating dictionaries
dict1 ={'color':'blue','shape':'square','volume':40}
dict2 ={'color':'red','edges':4,'perimeter':15}# Creating new pairs and updating old ones
dict1['area']=25# {'color': 'blue', 'shape': 'square', 'volume': 40, 'area': 25}
dict2['perimeter']=20# {'color': 'red', 'edges': 4, 'perimeter': 20}# Accessing values through keys - an KeyError will occur if the key does not existsprint(dict1['shape'])# You can also use get, which doesn't cause an exception when the key is not found
dict1.get('false_key')# returns None
dict1.get('false_key',"key not found")# returns the custom message that you wrote# Delete item key and return the value if the key does not exists a KeyError occursprint(dict1.pop('volume'))# Merging two dictionaries
dict1.update(dict2)# if a key exists in both, it takes the value of the second dict
dict1 # {'color': 'red', 'shape': 'square', 'area': 25, 'edges': 4, 'perimeter': 20}# Getting only the values, keys or both (can be used in loops)
dict1.values()# dict_values(['red', 'square', 25, 4, 20])
dict1.keys()# dict_keys(['color', 'shape', 'area', 'edges', 'perimeter'])
dict1.items()# dict_items([('color', 'red'), ('shape', 'square'), ('area', 25), ('edges', 4), ('perimeter', 20)])# create a shallow copy of dict1
dict3 = dict1.copy()# dict3 = {'color': 'red', 'shape': 'square', 'area': 25, 'edges': 4, 'perimeter': 20}
a_number =3
the_complete_number = a_number + a_number #result should be 6
the_the_complete_number = a_number * a_numner #result should be 9
the_the_the_complete_number = a_number - a_number #result should be 0
#math in python: Multiple action
x =int(input("Type value for x: "))
y =int(input("Type value for y: "))
z =int(input("Type value for z: "))print(x / y + z)#you need multiple solution at once.#Go ahead and copy the code to your# .py script to see the results.
x =int(10)
y =int(5)print(x + y)#you need to define integer extra otherwise# python will count this as a str and won't work.#copy the code to your py script to see the accurate result.
number_1 =2
number_2 =5
total_number = number_1 * number_2
#the result should be 10 :)
total_number2 = number_2 - number_1
#result should be 3 :)
number3 = number_1 + number_2
# result should be 7
#math in python: Multiple action
x =int(input("Type value for x: "))
y =int(input("Type value for y: "))
z =int(input("Type value for z: "))print(x * y + z)#you need multiple solution at once.#Go ahead and copy the code to your# .py script to see the results.