Formatted output
%: Placeholder
S: string
D: Digital
Percent: Indicates a%, the first% is used to escape
Instance:
Name = input (' name: ') age = Int (input (' Age: ')) print (' My name is%s, my age:%d, my study progress 3%%. '% (name, age) ') # Execution Result: # Name: hkey# Ages: 20# My name is hkey, I Age: 20, my study progress 3%.
Initial encoding
The original code was proposed by the United States, when the ASCII code was only used to store letters and symbols, and later in order to solve the differences in the globalized text, created the Universal code: Unicode
In Unicode,
1 bytes representing all English, special characters, numbers, etc.;
A Chinese needs 4 bytes, 32 bits is a waste.
Later, upgrading from Unicode to Utf-8, UTF-8 is one of the ways Unicode is implemented
In Utf-8, a literal is stored in 3 bytes.
00000001 8 bit bit = = 1 bytes (byte)
1byte 1024byte (bytes) = = 1KB
1KB 1024KB = = 1MB
1MB 1024MB = = 1GB
1GB 1024GB = = 1TB
And or not logical judgment
Judgment priority (FOCUS): () > Not > and > or
Exercise 1: Judging the results returned below (hint: judging by () > Not > and > or)
1,3>4 or 4<3 and 1==1# 3>4 or false# false2,1 < 2 and 3 < 4 or 1>2 # True or 1>2 # true3,2 > 1 and 3 < 4 or 4 > 5 and 2 < # True or false# true4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8# False or False or 9 < 8# false5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6# false or FALSE and 9 > 8 or 7 < 6# false or False or 7 < 6# false or false# False6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6# false and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6# false or FALSE and 9 ; 8 or 7 < 6# false or False or 7 < 6# false or false# false
The above is conditional judgment, can also be directly to judge the number:
x or y x is nonzero, returns x, otherwise returns y
Print (1 or 2) # 1print (3 or 2) # 3print (0 or 2) # 2print (0 or 100) # 100 when the or preceding number is not 0, the preceding number is returned; When the number in front of or is 0 , the following number is returned.
X and y X are true, return y, opposite or exactly
Print (1 and 2) # 2print (0 and 2) # 0 Returns the following number when the number in front of and is not 0, or 0 if the number in front of and is 0.
Conversions between numbers and Boolean values follow the following two rules:
(1) Number converted to bool: Non 0 to bool value: True;0 converted to bool value: False
(2) The bool value is converted to a number: True: 1; False is 0
Job Title:
1. Use the while loop to enter 1,2,3,4,5,6 8,9,10
2. Ask for 1-100 of all numbers and
3. All odd numbers of output 1-100
4. All even numbers of output 1-100
5.1-2+3-4+5 ... 99 of all numbers of the and
6. User Login (three chance retry)
#!/usr/bin/python3#-*-coding:utf-8-*-#Author:hkey#Job Title:#1. Use the while loop to enter 1,2,3,4,5,6 8,9,10Count=0 whileCount < 10: Count+ = 1#equivalent to Count = Count + 1 ifCount = = 7: Continue #continue end this cycle, start the next loop, continue the following code is no longer executed Print(count)#2. Ask for 1-100 of all numbers andNum=0 forIinchRange (1, 101):#Range takes a range of 1, 100 of all numbers, through the for loop to iterate the additionnum + =IPrint(num)#3. All odd numbers of output 1-100Print(List (range (1, 101, 2)))#4. All even numbers of output 1-100Print(List (range (2, 101, 2)))#5.1-2+3-4+5 ... 99 of all numbers of the andsum =0count= 1 whileCount < 100: ifCount% 2:#The count is 0 if the condition is not true, the count is even, and the count to 2 takes the remainder if the condition is not 0, which indicates that count is an odd number.Sum + = Count#Odd do addition Else: Sum-= Count#even do subtractionCount + = 1Print(sum)#Summary:#in bool value, 0 None null is False, others are True#6. User Login (three chance retry)Count =0 whileTrue:user= Input ('Username:') PWD= Input ('Password:') ifuser = ='Admin' andPWD = ='123': Print('Login successful.') Break Else: Print('The user name password is incorrect, please try again. ') Count+ = 1ifCount = = 3: Print('Login verification exceeded three times and login failed.') Break
answers to homework questions
[Python] formatted output, character set, And/or/not logic judgment