Python基础学习(一) ------ print / 基本的数学运算

一. 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
""""

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值