0% found this document useful (0 votes)
2 views

Python Cheatsheet

The document outlines basic programming concepts, including problem definition, solution planning, algorithm translation, error removal, and program organization. It includes various quizzes and examples related to string manipulation, decision structures, loops, and data types in Python. Additionally, it discusses the importance of constructors and provides insights into syntax, runtime, and logic errors.

Uploaded by

Tytyt Ttyt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Cheatsheet

The document outlines basic programming concepts, including problem definition, solution planning, algorithm translation, error removal, and program organization. It includes various quizzes and examples related to string manipulation, decision structures, loops, and data types in Python. Additionally, it discusses the importance of constructors and provides insights into syntax, runtime, and logic errors.

Uploaded by

Tytyt Ttyt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Basicprogramming

Struction
Program planning includes: T 4.) OUTPUT: numbers from 0 to 999,999 Quiz L11
1 Define the problem L3 String Variables >>Var1=“hello” >>print(var1) Quiz L6 1.) The __init__ method (also known as the
2 Plan the solution to the problem, hello 1.) 3+4>len(“CityU”) will return TRUE.
3 Translate the algorithm into a programming language,
constructor) is automatically called when an
4 Locate and remove any errors in the program,
2.) The following is evaluated to be FALSE. instance is created. TRUE.
5 Organize all the material that describes the program.
average 2.)
6.) This is an infinite loops TRUE
Sequeee 3.) The condition not (x<=2 and x%3!=0) is logically
equal to x>2 or x%3==0

>>str1=“spam & eggs” >>str1[0] ‘s’ >>str1.find(‘g’ ) 8


>>str1.rfind(‘g’) 9. >>str1.rfind(‘gs’) 9. >>str1.find(‘huh’) -1 =
4.) output Lee
cannot find

7.) Output is 3.) To find the circumference of ins below, one can
* use . A. ins.circumference() C.
** Circle.cirumference(ins)
Quiz ***
>>str1=“spam & eggs” >>str1[-5:-2] ‘eg’ >>str1[0:-1] 4.) A class is a template from
The following is an example of a decision structure.
if risk loving ‘spam & eggs’ =-1-1=-2 which objects can be
start a company Str[m:n:s]. S:step (+:forward: -backward) created. It specifies the
else attributes (properties) and
find a regular job. \backslash 5.) Suppose x is a variable with numeric value and there are methods associated with
two conditions: 1) x>10 2) 1<x<=5 Quiz L9
L2 numbers = 2line instances (objects) in that
if x>10: 1.) A function is said to return its output. class. TURE
// return the quotient (the integer party商)%餘數 change to 1 ……. 2.) OUTPUT: a H -

remainder(modulo operation) line elif 1<x<=5 3.) Assigns value 3 to the variable y. This associated with area & init
…….. & circumference.
=>

>>help(abs) #help on built-in function abs in module builtins: def fun(x):


6.) ‘a’<‘b’ compares the ASCII values for ‘a’ and ‘b’. TRUE. return x+1
>> abs(x,l) e.g., abs(-23) =23 絕對正值 >>> round (5.15, 1) =5.2
>>import math >>math.pi >>math.e >>math.ceil(3.00001)=4 x
7.) if x>20:
…….
y=fun(2) -output 14 Will Print
變int, 4 X >>math.floor(3.99999)=3 x變int, 3 x Eelif x>10: TT
>>import random >>random.randint(1,100) 隨便從1-100抽⼀個數字 …….. D
OUTPUT >>print(“a”,”b”, sep=“&”) a&b Given the above, replacing the condition of the elif part by

H
Object Types>>type(6+2) <class ‘int’> >> type(“32”) <class ‘str’> >>print(“a\tb”.expandtabs(7) 7格空格 x>10 and x<=20 will not affect the logic of the script 7.) Variable pi can be
>>type(6+2.0) <class ‘float’> >>type(6/2) <class ‘float’>
>>int(-2.7) = -2 不會round up但轉int but import math >>print(“a\n\nb”) a b \n跳下⼀⾏ accessed via

output
>>math.floor(-2.7)=-3 - 32X - A. insB.pi. B. insA.pi
Quiz L3 Quiz L7
3
int
>>type ("3" ) <class 'str'> s
>>x=int ("3") >>print (x) 3 1. “Woohoo”.rfind(“oo”) will return the index of which (highlighted) 1.) Calculate the summation from 1 to 50.
>>type (x) <class 'int'> >>int(“3.0”) ERROR ‘o’?
① 7.) Function header must end with:
>>float(3) 3.0 >>float(“3”) 3.0 >>float(“4/2”) ERROR 2 "good study"[20 : : -3] will get the substring "ytdg" from the Functions may not have explicit
STR function. >>type(3.4) = float >>str(3.4)=‘3.4’ return statements
>>num=input(“a no.) >>num=float(input(“a no.)) num-float string "good study".
3. print("a\n/tb ".lstrip(), "A" ,sep="@") will return Functions can be return T/F.
num = Str
-

a
1Syntax Errors錯咗符號,或者no name a variable /tb*@A.
ERROR TYPE
3
contputy
2.) Display all even numbers between 1 and 10000. = Quiz L12
4. Any code enclosed in a pair of parentheses can span multiple lines. False 1.) C.) [i%4 for i in range(4) ]
2Runtime Errors (Traceback搵唔到之前嘅record) 3Logic Errors (most difficult to find) Quiz L10
5.In string concatenation, “a”+“b” is the same as “b”+“a” . True 1.) A class is a template from which objects can be created. It specifies the D.) [i%4 for i in range (4,8)]
6. If str1 is a string, then str1[m:n] is the substring beginning at 8.) When one loop is contained in the properties (attributes) and methods that are common to all objects (instances)
WILL generate [0,1,2,3]
of that class. True
position m and ending at position n . False body of another loop, it is said to be 2.) Writes three lines to output.txt, overwriting
Quiz L2 2.)
7.”BCDE”.find(‘d’) will return -1.

ysmethods
1 In python, we define x=“7.0” Then x is a string. nested. any existing content
8. The negative index of ‘y’ in “CityU” is -2. 3.) The continue statement causes the current iteration
2 num=input(“Give me a number:”). If a user enters 5.0, (
of the body of a loop to terminate, and the execution
the num is a string. Quiz L4 returns to the loop’s header. 3.) Will return
"
3 In python, print(2+3.0,3*3) will return 5.09 1.slicing tuples will generate error is FLASE. 4.) A for loop is a repetition structure. TRUE
4 8\2 will generate a float —> False 2.After running through the following script, the index of 'CityU' in lst is 1. “Husky\nLabrador\nPoodle\nRottweiler\n"
5 print(“Hello World!”} is a Syntax Error University_name = "CityU"
5.)
output 3, 4 6 7 9 , 10
. . .

lst = ["PolyU", University_name, "HKU"] 6.) Display the string “CityU” a hundred
6 The following will return 3.0 times. 3.) The __init__method (also known as the constructor)
x=9 x**0.5 print(x) lst.insert(1, "HKUST")
lst[1], lst[2] = lst[2], lst[1] is automatically called when an object is created. TURE.
7 Which one is variable name=letter, no, underscores_ 3. If x is a nonempty list , then x[0]=“CityU” will update part of x.
A. Var-name BTrue C1variable D_myVariable 4.An item inside a list can be a string, tuple.
Kahoot 2 5.For a list with at least two items, the index of the second from the left is 1.
6.Give the tuple t = ('a', 'b', 'c'), which of the following operations are
1 The return value of round (368.-1) is 370
Quiz L5 valid? Choose all that apply. t[-1:]+t[:2] list(t[0:2])

output
1.) 3<“3” will generate an error message. 7.The statement s=‘2’+’3’ and t=“ “.join([‘2’,’3’]) assign the same
2.) Object True and False are said to have data type: Bool value to the variables s and t. TRUE.
3.) chr(“T”) will return the ASCII value of “T”. TRUE. Quiz L8 1.)When will the following loop terminate? 4.) infile can be renamed to something else.
4.) Character comparison is based on comparisons of their ASCII values. keep_it_real is equal to -1. 3 7.) set.intersection (set2) is equal to the set
TRUE
2.) the value of num is 16. -T containing elements in both set1 and set 2.
5.) [9, “tom”,1]>[9, “Tom”,8] will generate TRUE.
6.) [1,2,3,4]<[4,3,2,1] will generate TRUE.
num=2
While num<12: num*2
4 -F + T

7.) [1,[2,3]]>[1,2,3] will generate an error message.


6.) Will return [‘Python Programming’,’Data
Science’,’Machine Learning’]

You might also like