0. In Python, int represents an integer
bool: Boolean type
float: floating point type
str: string type
1. Why are TRUE and FALSE of boolean type (bool) represented by 0 and 1 respectively?
Computers only know binary. Since binary has only two numbers, 0 and 1, it is appropriate to use 0 and 1 to represent it, because there is no need to waste resources in the process of conversion
2. Use int() to convert a decimal to an integer, is the result rounded up or down?
round down
3. Human thinking is accustomed to the "rounding" method. Is there any way to make int() round up according to the "rounding" method?
add 0.5
For example: 8.3----8, int(8.3+0.5)=8
8.6----9,int(8.6+0.5)=9
4. The type of a variable to be taken. In the video, you can use type() and instance(), which one do you prefer?
type() directly returns the type of an input variable
ininstance() returns the type comparison of a variable with another variable, and returns TRUE if it is of the same type, and returns FALSE if it is different
5. Python3 can name variables in Chinese, do you know why?
Python3 source files use UTF-8 encoding by default (supporting Chinese), which makes the following code legal
hands-on
0.
s.isalnum() All characters are numbers or letters, return True, otherwise return False
s.isalpha() All characters are letters, return True if true, otherwise return False
s.isdigit() All characters are numbers, for True returns True, otherwise returns False
s.islower() All characters are lowercase, true returns True, otherwise returns False
s.isupper() All characters are uppercase, true returns True, otherwise returns False
s.istitle() All words are capitalized, return True if true, otherwise return False
s.isspace() All characters are blank characters, return True if true, otherwise return False
6. Determine whether a given year is a leap year
Written by myself: need to improve the places less than 0 and greater than 3000
while True:
temp = input('Enter the year')
while temp.isspace():
temp=input('The input is incorrect, please re-enter')
year = int(temp)
if year<0 or year>3000:
print( 'The input year is invalid')
else:
if year%4==0 and year%100!=0 or year%400==0:
print('Leap year')
else:
print('Not a leap year')
Attach the code for the small turtle:
temp = input('Please enter a year:') while not temp.isdigit(): temp = input("Sorry, your input is incorrect, please enter an integer: ") year = int(temp) if year/400 == int(year/400): print(temp + ' is a leap year!') else: if (year/4 == int(year/4)) and (year/100 != int(year/100)): print(temp + ' is a leap year!') else: print(temp + 'Not a leap year!')