python data types and operators

A, python type judgment

type,isinstance

type (variable or constant): return data type

a = 23.3
print(type(a))
b = 2e3
print(b, type(b))
输出:

<class 'float'>
2000.0 <class 'float'>

 

the isinstance (variable or constant, the data type name) to determine the data type. If the specified data type returns true, false otherwise

10 = A 
IF the isinstance (A, int):
Print ( "A is an integer")

Two, Python built-in type into the data type (type itself supported language) and custom type, class (class).

Common built-in type comprising:

1. Value Type

  • Integer int, is an integer type. For example: 3,4,5
  • Float float, on behalf of real numbers. 2 has written: NATURAL notation and scientific notation

Natural notation: 2.3,4.5

Scientific notation: 2e2 represent 200.e refers to the base 10 exponent, e must follow behind an integer

a = 23.3
print(type(a))
b = 2e3
print(b, type(b))
输出:

<class 'float'>
2000.0 <class 'float'>

  • A plurality of complex 2 + 3j
2J. 3 = + C 
Print (C, type (C))
Output:
(+ 2J. 3) <class 'Complex'>

2. Boolean

Boolean type represents true or false, right or wrong, yin and yang, etc., as long as the two states can be represented by the Boolean type. It has two values: True (1) and False (0)

Arithmetic and Boolean values which can be performed
A =. 3
B = 3.5 of
C = True
Print (A + C)
Print (C *. 5)

3. string type

Quotes is a string

'Single quoted strings'

'Double-quoted string'

'''

Multi-line strings

'''

 

"""

Multi-line strings

 """

 

Escape string, if the single quotes include a single quote then need to escape single quotes

Escape character description escape character description

\'It represents one ordinary single quote character

\ r carriage return

\"It represents an ordinary character in double quotes

\ n newline

\'''A general-purpose three single quotes

\\An ordinary character

\ \"""An ordinary three double quotes

\ t the Tab key

Third, the operator and the expression (Expression no value)

1. Arithmetic operators include: + - * / // (divisible)% (modulo take the remainder) ** (index)

Modulo operator uses: determining parity determines whether divisible by the greatest common divisor number daffodils

2. Functions

  • Mathematical Functions

 

 

 

  • Random function

Introducing random random function library

Random Import 
Print (the random.randint (1,100)) random integer between 1 and 100, the packet before packet
print (random.random ()) random decimal between 0 and 1, before and after the package does not include
print (random. randrange (1,100,2)) between 1 and 2 in steps 100 outputs a random number, excluding the packet front
output:
33
.05559590353134125
21

 

 

 3. assignment operator

= += -= *= /= 

 

 

 赋值运算符左边必须是变量

4.关系运算符

关系运算符就是比较运算符,结果是布尔值

 

 

 练习:

1.将华氏温度转换为摄氏温度

C = float(input())
F = 1.8 * C + 32
print(F)

2.求解一次方程ax+b=0,输入a和b的值,求解x

a = int(input("请输入一次方程的系数a:"))
b = int(input("请输入一次方程的常量b:"))
print("一次方程 %dx+%d=0 的根是:x=%d" %(a, b, (0 - b)/a))
输出

请输入一次方程的系数a:2
请输入一次方程的常量b:6
一次方程 2x+6=0 的根是:x=-3


3.编写一程序,接收从键盘上输入的2个学生的姓名、数学成绩、计算机成绩,粉笔存入6个变量中,然后打印出这2个学生的姓名和总成绩

name1 = input("请输入同学a的名字:")
name2 = input("请输入同学b的名字:")
core1 = float(input("请输入同学a的数学成绩:"))
core2 = float(input("请输入同学b的数学成绩:"))
core3 = float(input("请输入同学b的计算机成绩:"))
core4 = float(input("请输入同学b的计算机成绩:"))
core_all = core1 + core2 + core3 + core4
print("同学%s和同学%s的总成绩为:%.0f" %(name1, name2, core_all))
print("同学%s和同学%s的总成绩为:%.2f" % (name1, name2, (core1 + core2 + core3 + core4)))


4.编写一程序,对用户输入的两个数据a和b,输出交换后的a和b的值
a = int(input("请输入值a:"))
b = int(input("请输入值b:"))
a, b = b, a
print("分别输出a和b的值:%d,%d" %(a, b))

5.从控制台输入2个数,输出较大的值
a = int(input("请输入值a:"))
b = int(input("请输入值b:"))
print("请输出最大值:%d" % max(a, b))

6.从键盘输入一个整数,判断这个数能否被3整除
a = int(input("请输入值a:"))
if a % 3 ==0:
print("能被3整除")

7.从键盘输入一个3位数,求这三位数各个数字的和
a = int(input("请输入三位整数a:"))
gewei = a % 10
shiwei = a // 10 % 10
baiwei = a // 100
print("各个位数之和为:%d" % (gewei + shiwei + baiwei))

8.从控制台输入一个三位数,如果是水仙花数,就打印 “是水仙花数”,否则打印“不是水仙花数”
a = int(input("请输入三位整数a:"))
gewei = a % 10
shiwei = a // 10 % 10
baiwei = a // 100
if gewei**3 + shiwei**3 + baiwei**3 == a:
print("是水仙花数")
else:
print("不是水仙花数")

Guess you like

Origin www.cnblogs.com/houjiashan/p/12165864.html