Info1110 Cheatsheet
Info1110 Cheatsheet
Info1110 Cheatsheet
Abyan Majid
1 Terminal commands
1
3 y : int = 4.25 # with type a n n o t a t i o n
4 m , n = ( " cool " , " shorthand " ) # i n i t i a l i z e m u l t i p l e vars in a single line
5
8 def some_function () :
9 """
10 d o c u m e n t a t i o n of s o m e _ f u n c t i o n ()
11 """
12
13 print ( x )
14 print (m , n )
15 print ( __name__ ) # _ _ n a m e _ _ fetches the name of current module .
16 print ( some_function . __doc__ ) # __doc__ fetches d o c u m e n t a t i o n of s o m e _ f u n c t i o n ()
1 4.25
2 cool shorthand
3 __main__
4
5 documentation of some_function ()
3 Strings
3.1 Ways to format strings: concatenation, multiple args, format(), f-string, %s
3 # String c o n c a t e n a t i o n
4 print ( " Hello " + " , " + " World ! " )
5
9 # format () method
10 print ( " {} , {}! " . format (a , b ) )
11
15 # using % s
16 print ( " %s , % s ! " %( a , b ) )
1 Hello , World !
2 Hello , World !
3 Hello , World !
4 Hello , World !
5 Hello , World !
3.2 Functions/methods for strings: capitalize(), upper(), lower(), len(), strip(), re-
place(), count(), isalpha(), isnumeric()
2
1 print ( " hello , wORld ! " ) # prints to the t e r m i n a l
2 print ( " hello , wORld ! " . capitalize () ) # c a p i t a l i z e s the first c h a r a c t e r
3 print ( " hello , wORld ! " . upper () ) # u p p e r c a s e s all c h a r a c t e r s
4 print ( " hello , wORld ! " . lower () ) # l o w e r c a s e s all c h a r a c t e r s
5 print ( len ( " hello , wORld ! " ) ) # get length of string
6 print ( " hello , wORld ! " . strip ( " hello , " ) ) # removes " hello , " from string
7 print ( " hello , wORld ! " . replace ( " hello " , " Bye " ) ) # r e p l a c e s " hello " with " Bye "
8 print ( " hhhh heeello ooo ! " . count ( " h " ) ) # counts the number of " h " in the string
9 print ( " 100 " . isalpha () , " hello " . isalpha () ) # checks if all char are in a l p h a b e t
10 print ( " 100 " . isnumeric () , " hello " . isnumeric () ) # checks if all char are numeric
1 hello , wORld !
2 Hello , wORld !
3 HELLO , WORLD !
4 hello , world !
5 13
6 wORld !
7 Bye , wORld !
8 5
9 False True
10 True False
1 H
2 !
3 Hello ,
4 World !
5 llo ,
6 Hlo ol !
4 Operators
4.1 Arithmetic operators
1 a , b = (10 , 4)
2 print ( a + b ) # addition
3 print ( a - b ) # subtraction
4 print ( a * b ) # multiplication
5 print ( a / b ) # division
6 print ( a ** b ) # exponentiation
7 print ( a % b ) # modulus / get r e m a i n d e r
8 print ( a // b ) # floor d i v i s i o n / get q u o t i e n t
9 print ( a ^ b ) # bitwise XOR ( prob not a s s e s s e d )
3
1 14
2 6
3 40
4 2.5
5 10000
6 2
7 2
8 14
1 a = 1
2 b = 2
3 c = b
4 print ( a == b )
5 print ( a != b )
6 print ( a < b )
7 print ( a > b )
8 print ( a <= c )
9 print ( a >= c )
10 print ( not ( a == b ) )
11 print (( b == c ) and ( a != b ) )
12 print (( b != c ) or ( a == b ) )
1 False
2 True
3 True
4 False
5 True
6 False
7 True
8 True
9 False
4
12 print ( ord ( " A " ) ) # char to ASCII
13 print ( hex (128) ) # decimal to hex
14 print ( oct (56) ) # decimal to octal
1 temperature = 25
2
10 # nested ifs
11 if temperature > 20:
12 if temperature < 30:
13 print ( " Wear a jacket " )
14 else :
15 print ( " You may not wear a jacket " )
5
12
13 # i n f i n i t e loop ( d i s c o u r a g e d unless a b s o l u t e l y n e c e s s a r y )
14 while True :
15 # do stuff
1 0
2 1
3 2
4 Enter a value : #6
5 You entered : 6
6 Enter a value : #8
7 You entered : 8
8 Enter a value : # exit
9 You entered : exit
8 Functions
8 arg1 = 5
9 arg2 = 7
10 print ( multiply ( arg1 , arg2 ) )
11 print ( return_none ( arg1 , arg2 ) )
1 35
2 None
√
9 Math library: e, π, x
3 e = math . e # euler ’s c o n s t a n t
4 pi = math . pi # pi
5 root_of_5 = math . sqrt (5) # square root f u n c t i o n
6
7 print ( e )
8 print ( pi )
9 print ( root_of_5 )
10 print ( math . ceil (5.6) )
11 print ( math . floor (5.6) )
1 2.718281828459045
2 3.141592653589793
3 2.23606797749979
4 6
5 5