[go: up one dir, main page]

0% found this document useful (0 votes)
29 views6 pages

Info1110 Cheatsheet

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Info1110 Cheatsheet

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

INFO1110 Cheatsheet

Abyan Majid

August 30, 2023

Checklist for assessment 1 preparedness

□ Flowcharts □ comments, docstring □ +, -, *, /, **, %, //, □ count-controlled


ˆ (bitwise XOR) while, event-
□ Terminal commands □ capitalize(), upper(),
controlled while
lower(), strip(), re- □ if, else, elif, nested ifs
□ Variables, typecast- place() □ math lib: e, pi,
ing, inputs □ string indexing,
□ len(), count(), type() sqrt(), ceil, floor
□ Type annotation string slicing
□ isalpha(), isnu- □ chr(), ord(), hex(),
□ name , doc meric() □ function parameters, oct()
□ and, or, not arguments, return
□ concatenation, multi- type, body □ num:.xf, num:xd
ple print args, for- □ ==, !=, <, >, <=,
mat(), f-string(), %s >= □ return, pass, None □ debugging: pdb, IDE

1 Terminal commands

1 $ python3 < filename >. py # i n t e r p r e t and run f i l e n a m e . py


2 $ python3 -m py_compile < filename > # create a . pyc e x e c u t a b l e binary
3 $ python3 # enter python i n t e r p r e t e r
4 $ pwd # see current d i r e c t o r y
5 $ ls # see c o n t e n t s of current d i r e c t o r y
6 $ mkdir < dirname > # create new d i r e c t o r y
7 $ cd < dirname > # n a v i g a t e to another d i r e c t o r y
8 $ touch < filename > # create a new file
9 $ mv < filename > < destination > # move a file to another d i r e c t o r y
10 $ cp < filename > < new_filename > # copy a file
11 $ vim < optional : filename > # enter vim , exit with : q

2 Variables, type annotation, name , doc


On variable naming conventions: Be descriptive, lowercase everything, and write spaces as underscores

1 # Python can infer datatypes , hence you are not r e q u i r e d to a n n o t a t e types .


2 x = 10

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

6 x = y # "=" is assignment , not e q u a l i t y !


7

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

1 a , b = ( " Hello " , " World " )


2

3 # String c o n c a t e n a t i o n
4 print ( " Hello " + " , " + " World ! " )
5

6 # Passing m u l t i p l e args into print ()


7 print ( " Hello , " , " World ! " )
8

9 # format () method
10 print ( " {} , {}! " . format (a , b ) )
11

12 # f - string ( using string l i t e r a l s )


13 print ( f " { a } , { b }! " )
14

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

3.3 Indexing strings

1 print ( " Hello , World ! " [0]) # print 1 st char


2 print ( " Hello , World ! " [ -1]) # print last char
3 print ( " Hello , World ! " [:6]) # print chars from the b e g i n n i n g until the 6 th ( e x c l u s i v e )
4 print ( " Hello , World ! " [6:]) # print the 6 th chars and onwards
5 print ( " Hello , World ! " [2:6]) # print 2 nd char until the 6 th ( e x c l u s i v e )
6 print ( " Hello , World ! " [::2]) # print a l t e r n a t e chars

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

4.2 Comparison and boolean operators


Comparison operators in python: ==, !=, <, >, <=, >=
Boolean operators in python: and, or, not

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

5 Inputs, typecasting, encoding: int(), float(), str(), bool(), chr(),


ord(), hex(), oct()

1 foo = input ( " Some input text : " ) # input


2 bar = float ( input ( " Enter a float : " ) ) # t y p e c a s t str input to float
3

4 int_of_bar = int ( bar ) # float to int


5 print ( int_of_bar )
6 print ( float ( int_of_bar ) ) # int to float
7 print ( str ( int_of_bar ) ) # int to str
8 print ( str ( bar ) ) # float to io str
9 print ( bool ( int_of_bar ) ) # int to bool , a n y t h i n g > 0 e v a l u a t e s to True
10 print ( bool (0) ) # int 0 to bool
11 print ( chr (97) ) # ASCII to char

4
12 print ( ord ( " A " ) ) # char to ASCII
13 print ( hex (128) ) # decimal to hex
14 print ( oct (56) ) # decimal to octal

1 Some input text : # Hello !


2 Enter a float : #3.14
3 3
4 3.0
5 3
6 3.14
7 True
8 False
9 a
10 65
11 0 x80
12 0 o70

6 Conditionals: if, elif, else, nested ifs

1 temperature = 25
2

3 if temperature > 30: # if


4 print ( " It ’s hot " )
5 elif temperature > 20: # else if
6 print ( " It ’s not that hot " )
7 else : # else
8 print ( " It ’s a bit chilly " )
9

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 " )

1 It ’ s not that hot


2 Wear a jacket

7 while loops: count-controlled, event-controlled, infinite loop

1 # count - c o n t r o l l e d while loop


2 count = 0
3 while count < 3: # stops looping when count >= 3
4 print ( count )
5 count += 1
6

7 # event - c o n t r o l l e d while loop


8 user_input = " "
9 while user_input != " exit " : # stops looping when u s e r _ i n p u t == " exit "
10 user_input = input ( " Enter a value : " )
11 print ( " You entered : " , user_input )

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

1 def multiply ( param1 : int , param2 ) : # type a n n o t a t i o n for p a r a m e t e r s is o m i s s i b l e .


2 return param1 * param2
3

4 def return_none ( param1 , param2 ) -> int : # return type a n n o t a t i o n


5 product = param1 * param2 # no return s t a t e m e n t !
6 # Therefore , f u n c t i o n returns None r e g a r d l e s s of return type a n n o t a t i o n < int >
7

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

1 import math # imports the math library into scope


2

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

You might also like