Python strings, built-in functions, numerical types (1)

1Basic data types

 (1).int theory can be infinite, there is no upper limit, but it is limited by the size of the machine memory.

 (2).flot (decimal)

  3.14 10/3 10//3 (except floor)

   Note: 10=3*3+1 

     10//3 -->3 quotient (integer)   

     10%3 --> 1 takes the remainder

(3). complex plural

(4).boolean Boolean type determines true (True) and false (flase)

2. Variable naming rules: _ or starting with a letter, including _, letters, numbers, and cannot be system built-in keywords

3.Python View built-in keywords

import keyword
print (keyword.kwlist)
a=keyword.kwlist
print(len(a))

    

4. = assignment symbol == equals symbol

 5. 3!=2 is not equal to 

               

                a=1

                a+=2 is equivalent to a=a+2

6. del deletes variables. For example: del a

7.         in , not  int

8. Base representation of numbers Weight

   Thousands and hundreds

   1     2  3    4      5          1*10000+2*1000+3*100+4*10+5*1

   binary

   101 From right to left, the weights are 2**0 + 2**1+ 2** (n-1)

     The number represents the base, position and the weight of this position.

    binary bin(2)

   Octal oct(9)

   Hexadecimal hex (17) 0-9 A 10 B 11 C 12 D 13 E 14 F 15

 

9. All input obtained are strings

x = input("请输入你的年龄:")
print(x)
print(type(x))
age=int(x)
print(age)
print(type(age))

10.

11. Output the print function (can accept multiple parameters, separated by commas)

12. The round function retains n decimal places, 4 is rounded to 6, 5 is judged by forwarding the odd number, and rounding the even number. (There is an error)

4.2  round(4.2)              4

4.26  round(4.26,1)       4.3

4.21  round(4.21,1)       4.2

4.25  round(4.25,1)       4.2

4.15  round(4.25,1)       4.1

precision pending decimal point

import  decimal
a = decimal.Decimal(1.2456)
b = a.quantize(decimal.Decimal("1.00"))
print(b)

 

Capability expansion

Assignment 1. Use the input function to record the content entered by the keyboard, print out the type of the value, and convert it to a numeric type.

     Determine the size of the value num. If num>=90, the output prints: Your score is excellent;

                                      If num>=80 and num<90, the output prints: Your score is average;

                                     If num<60, the output prints: Your score is unqualified.

score = int(input("请输入你的成绩:") )
if score>=90:
    level="优秀"
elif score>=80:
    level="良好"
elif 80>=score>=60:
    level="及格"
else:
    level="不及格"
print("你的成绩为%s"%(level))

 

Assignment 2. There are 23 boys and 15 girls in the class. Calculate the percentage of boys and girls in the total number of students in the class and keep two decimal places.

male,fmale = 23,15
totle= male+ fmale
rate_male = male  / totle
rate_fmale = fmale / totle

import decimal

str_rate_decimal = str(decimal.Decimal(rate_fmale*100).quantize(decimal.Decimal("1.00")))
print("男生占比:%.2f%%,女生占比:%s%%" % (rate_male*100,str_rate_decimal))

 

 

Guess you like

Origin blog.csdn.net/tjfsuxyy/article/details/84728888