1. Output hello world on the screen
print("hello world")
2. Try to define several variable types
int type float type bool type string type
a=1 b=1.0 c=2-1>2 d="f"
print(type(a),type(b),type©,type(d))
3. Try adding numeric types to see the result types
a=1 b=1.0 print(type(a+b))
4. Get the string entered from the keyboard and display it on the screen
a=input("Input:") for i in a: print(i)
5. Obtain the score entered on the keyboard, if the output is more than 60, if the output is less than 60, it will fail
a=int(input("Enter score:")) if a>60: print("pass") else: print("fail,")
6. Obtain the input level on the keyboard. If it is greater than 90 and less than 100, the output is excellent. If it is greater than 80 and less than 90, the output is good. If it is greater than 70 and less than 80, the output is medium. If it is greater than 60 and less than 70, the output is qualified. If it is less than 60, the output is unqualified
a=int(input("Input:")) if a>90 and a<=100: print("excellent") elif a>80 and a<=90: print("good") elif a>70 and a<=80: print("secondary") elif a>60 and a<=70: print("qualified") else : print("unqualified")
7. Define a list and output the elements in the list through the for loop
a=[2,4,6,5,7] for i in a: print("i")
8. Output 99 multiplication table through for loop
for i in range(1,10): for j in range(1,10): if i>=j: print("%s*%s=%s"%(i,j,i*j),end=" ") print(" ")
9. Complete bubble sorting and select sorting through for loop
a=[2,5,7,9,5,2] for i in range(len(a)): for j in range(len(a)-1): if a[j]<a[j+1]: a[j],a[j+1]=a[j+1],a[j] print(a) for i in range(len(a)): max=i for j in range(i+1,len(a)): if a[max]<a[j]: max=j a[i],a[max]=a[max],a[i] print(a)
10. Calculate 1 + 2 + 3 through the while loop +The combination of 100
i=1 sum=0 while i<=100: sum=sum+i i=i+1 print(sum)
11. Calculate 1! - 2! + 3! - 4!.. To 20
sum=0 for i in range(1,21): a = 1 for j in range(1, i + 1): a=a*j if i%2==0: sum=sum-a else: sum=sum+a print(sum)
12. Define a function to output hello world
def a(): b="hello world" return b print(a())
13. Define several functions, input through the user's keyboard, and select the function to execute through the IF statement machine.
a=int(input("Input:")) b=int(input("Input:")) def c(): return a+b def d(): return a-b if a>b: print(c()) else: print(d())
14. Define several functions, one of which is to receive the numerical type data input by the user, one is to sort the numerical value, and the other is to display the data in the list
a=input("Input:") b=[i for i in a] def b1(): for i in a: if i.isdigit(): return type(i) def b2(): b.sort() return b def b3(): for i in b: print(i,end=" ") print(b1()) print(b2()) b3()
15. Create an object with name attribute, create a eating method, output who is eating
class a: def __init__(self,name): self.name=name def eat(self): print(self.name) b=a("lqj") a.eat(b)
16. Create two objects, one inherits the other, object A name is an attribute, and there is A basketball playing method. Instantiate B to set the name to CAI Xukun, and call the basketball playing method,
class A: def __init__(self,name): self.name=name def Playbaseketball(self): print(f"{self.name}Good at basketball") class B(A): def __init__(self): super().__init__("Cai Xukun") b=B() b.Playbaseketball()
17. Create an object with date attribute, date and private. The initial value is [3,2,1,4]. In the common methods, there are methods for adding values to the list, sorting the list, and displaying the list.
class a: def __init__(self,__date:[3,2,1,4]): self.__date=__date def b1(self,i): self.__date.append(i) print(self.__date) def b2(self): self.__date.sort() print(self.__date) def b3(self): for i in self.__date: print(i,end=" ") b=a([3,2,1,4]) b.b1(3) b.b2() b.b3()
18. Receive the user's input from the keyboard, judge whether it is a string or a number. If it is a string, output the string. If it is a number, take the module 1024 on the original basis and then output it
a=input("Input:") if a.isdigit(): a=int(a) print(a%1024) elif a.isalpha(): print(a) else: pass
19. Write an exception handling so that the table below a list can be caught when it is out of bounds.
try: a=[2,3,4,6,7] for i in range(len(a)): for j in range(len(a)): a[j],a[j+1]=a[j+1],a[j] print(a) except IndexError: print("Subscript out of range error") finally: print("Happy learning")
20. Write an exception handling so that arithmetic errors and array out of bounds errors in the while loop can be handled correctly without interruption
i=0 while i<10: try: a = [2, 3, 4, 6, 7] for j in range(len(a)): a[j], a[j + 1] = a[j + 1], a[j] except IndexError: print("Subscript error") try: a = "2ff" b = int(a) except ValueError: print("Arithmetical error") finally: i=i+1
21. Read a text file and print the text file on the console
a = open(r"C:\Users\ASUS\Desktop\output\practice.txt", encoding="utf-8") b=a.read() print(b) a.close() a = open(r"C:\Users\ASUS\Desktop\output\practice.txt", encoding="utf-8") try: b=a.read() print(b) except: print("Read exception") finally: a.close()
5. Read a file, read 50 characters
a = open(r"C:\Users\ASUS\Desktop\output\practice.txt", encoding="utf-8") b=a.read(50) print(b) a.close()
22. Receive the data input by the user, and write a text file after input
a = open(r"C:\Users\ASUS\Desktop\output\practice.txt",mode="w", encoding="utf-8") b=input("Input:") a.write(b) a.close()
23. Copy a text file to a new text file
a = open(r"C:\Users\ASUS\Desktop\output\practice.txt", encoding="utf-8") b=a.read(50) c = open(r"C:\Users\ASUS\Desktop\output\jj.txt",mode="w", encoding="utf-8") c.write(b) c.close() a.close()
24. Copy a picture
a = open(r"C:\Users\ASUS\Desktop\lu.png","rb") b=a.read() c = open(r"C:\Users\ASUS\Desktop\output\new_lu.png","wb") c.write(b) c.close() a.close()