python学习Day1--基础知识

//数字与表达式
>>>2+2
4

>>>1/2
0

>>>1.0/2.0(1.0/2 | 1/2.0)
0.5

//执行整除,用双斜杠
>>>1//2
0

>>>1.0//2.0
0

>>>1%2
1

//十六进制和八进制
>>>0xAF
175

>>>010
8

//幂运算
>>>2**3
8

//设置变量
>>>x=3
>>>x*2
6

//语句
>>>print ("Hello,world!")
Hello,world!

//获取用户输入
>>>input("The meaning of life:")
The meaning of life:42
42

>>>x = input("x: ")
x:34
>>>y = input("y: ")
y:42
>>>print x*y
1428

//if语句(回车两次才能执行)
>>>if 1==2: print 'One equals two'
...
>>>if 1==1: print 'One equals one'
...
One equals one

//函数
>>>pow(2,3)
8
>>>abs(-10)
10
>>>round(1.0/2.0)//四舍五入
1.0

//模块
>>>import math
>>>math.floor(32.9)//向下取整、ceil向上取整
32.0

>>>int(math.floor(32.9))
32

//不用带上模板的名字(或者 foo=math.sqrt  用foo来代替)
>>>from math import sqrt
>>>sqrt(9)
3.0

//处理复数
>>>import cmath
>>>cmath.sqrt(-1)
1j

//字符串的拼接
>>>x = "Hello,"
>>>y = "world!"
>>>x+y
'Hello,world!'
>>>"Hello,"+"world!"

//长字符换行
>>>print '''This is 
a very long 
sentence'''
This is a very long sentence
>>>print "Hello,\
world!"
'Hello,world!'

//原始字符串
>>>print 'C:\nowhere'
C:
owhere
>>>print r'C:\nowhere'
C:\nowhere


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值