import math
print(math.floor(20.8))
20
from math import sqrt
print(sqrt(20))
``4.47213595499958
```python
from math import *
print(sin(3.14/2))
0.9999996829318346
x = 20 #整数类型变量
y = 40 # 整数类型变量
s = "I love python" # 字符串类型变量
t = 20.5 # 浮点类型变量
flag = True #布尔类型变量
print(flag)
print(x + y)
print(s)
print(t)
True
60
I love python
20.5
# 清空Python控制台
# import os
# import sys
# os.system('cls')
# f_handler = open('out.log','w')
# oldstdout = sys.stdout
# sys.stdout = f_handler
# os.system('cls')
# sys.stdout = oldstout
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in
6 sys.stdout = f_handler
7 os.system('cls')
----> 8 sys.stdout = oldstout
NameError: name 'oldstout' is not defined
5.2 % 2
print(3 +2 * -3**2 - (-3)**2)
print(4/2)
-24
2.0
print("binbin")
print(1+2)
sys.stdout = oldstdout
print(1+2)
x = 20
y = 50
k =12.4
print(x + y *k)
640.0
# 大整数,Java中的int,最大可以处理2^31-1(2147483647),-2^31(-2147483648)
# Python可以处理很大的数据
# print(2 ** 6000)
print(int("0b11010101",2))
print(int("0o43124",8))
print(int("0xFF12E",16))
213
18004
1044782
# 432
print(bin(432))
0b110110000
print(oct(432))
0o660
print(hex(432))
0x1b0
print(bin(0xFF12E))
print(hex(0b11111111000100101110))
``
```python
print(0b1011101)
print(0xFF12E)
1044782
print(0b11010101)
213
x = 1234.56789
#print(format(x, ‘0.2f’))
#print(format(x,‘12.1f’)) or print(format(x,’>12.1f’))
#print(format(x, ‘<12.2f’),12)
print(format(x, ‘0>12.2f’))
print(format(x, ‘0<12.1f’))
print(format(x, ‘0^12.1f’))
#print(format(x, ‘0.2e’))
name = input("请输入你的名字:")
age = input("请输入你的年龄:")
salary = float(input("请输入你的收入:"))
print("姓名:",name)
print("年龄:", age)
print("收入:", format(salary,'0.1f'))
请输入你的名字:王宇
请输入你的年龄:31
请输入你的收入:2000
姓名: 王宇
年龄: 31
收入: 2000.0
print(pow(2,3))
print(abs(-33))
print(round(3.7))
print(round(3.3))
from cmath import sin
print(sin(3.14))
8
33
4
3
(0.0015926529164868282-0j)
print("hello \"w\"orld")
print("hello \'w\'\"o\"rld")
print("Let's go!")
print('"OK"')
print("'H',\"W\"")
hello "w"orld
hello 'w'"o"rld
Let's go!
"OK"
'H',"W"
print("Hello","world","世界您好")
x = "Hello"
y = "World"
print(x + y + x)
Hello world 世界您好
HelloWorldHello
# 一、保持字符串的原汁原味
print("Hello\nWorld")
print(str(1234) + "\n" + str(4321))
# 方法1:使用repr函数
print(repr("Hello\nWorld"))
# 方法2:使用转义符输出\
print("Hello\\nWorld")
# 方法3:在字符串前面加r
print(r"Hello\nWorld")
Hello
World
1234
4321
'Hello\nWorld'
Hello\nWorld
Hello\nWorld
# 长字符串
print("""Hello
World
""")
print("Hello \
\
World")
Hello
World
Hello World
#实战与练习
# 1、请将下面的数值转换成另外3种进制,并使用print函数输出转换结果。例如
#(1)12345
#(2)0xF98A
#(3)0b1100010110
print(bin(12345))
print(oct(12345))
print(hex(12345))
print(bin(0xF98A))
print(oct(0xF98A))
print(0xF98A)
print(int("F98A",16))
print(oct(0b1100010110))
print(hex(0b1100010110))
print(int("1100010110",2))
0b11000000111001
0o30071
0x3039
0b1111100110001010
0o174612
63882
63882
0o1426
0x316
790
2、现在有一个变量x,值为5423.5346,使用format函数对该变量进行格式化,并使用print函数输出如下的5个格式化后的值。
x = 5423.5346
print(format(x, '0.3f'))
print(format(x, '0>10.2f'))
print(format(x, "0<10.2f"))
print(format(x, '0>10,.2f'))
print(format(x, '0^10,.2f'))
5423.535
0005423.53
5423.53000
005,423.53
05,423.530