一.
print
1.print
字符串 要加''
或者""
>>> print('hello world')
'''
hello world
'''
>>> print("hello world 2")
'''
hello world 2
'''
2.print 字符串叠加
>>> print('Hello world'+' Hello Hong Kong')
"""
Hello world Hello Hong Kong
"""
3.简单运算
可以直接print
加法+
,减法-
,乘法*
,除法/
. 注意:字符串不可以直接和数字相加,否则出现错误。
>>> print(1+1)
"""
2
"""
>>> print(3-1)
"""
2
"""
>>> print(3*4)
"""
12
"""
>>> print(12/4)
"""
3.0
"""
>>> print('iphone'+4) #字符串不可以直接和数字相加
"""
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
print('iphone'+4)
TypeError: Can't convert 'int' object to str implicitly
"""
int()
和 float()
;当int()
一个浮点型数时,int
会保留整数部分,比如 int(1.9)
,会输出1
,而不是四舍五入。
>>> print(int('2')+3) #int为定义整数型
"""
5
"""
>>> print(int(1.9)) #当int一个浮点型数时,int会保留整数部分
"""
1
"""
>>> print(float('1.2')+3) #float()是浮点型,可以把字符串转换成小数
""""
4.2
""""
二.数学运算
1.基本的加减乘除
python
可以直接运算数字,也可以加print
进行运算.
>>> 1+1
2
>>> 2-1
1
>>> 2*3
6
>>> 4/3
1.3333333333333333
2.^ 与 ** (幂方)
>>> 3**2 # **2 表示2次方
""""
9
""""
>>> 3**3 # **3 表示3次方
""""
27
""""
>>> 3**4
""""
81
""""
3.取余数 %
>>> 8%3
"""
2
"""
三.变量
1.自变量命名规则
可以将一个数值,或者字符串串附值给自变量,如apple=1
中,apple
为自变量的名称,1
为自变量的值。 也可以将字符串赋值给自变量 apple='iphone7 plus'
apple=1 #赋值 数字
print(apple)
""""
1
""""
apple='iphone 7 plus' #赋值 字符串
print(apple)
""""
iphone 7 plus
""""
2.如果需要用多个单词来表示自变量,需要加下划线,如apple_2016='iphone 7 plus'
请看代码
apple_2016='iphone 7 plus and new macbook'
print(apple_2016)
""""
iphone 7 plus and new macbook
""""
3.一次定义多个自变量 a,b,c=1,2,3
。
a,b,c=11,12,13
print(a,b,c)
""""
11 12 13
""""