百人计划

Python学习笔记1——Python基础

2017-03-10  本文已影响30人  qianyewhy

Python基础

操作符:

**  指数                    2**3=8

%    取模、取余数  22%6=6

//  整除商数取整     22//8=2

/    除法                    22/8=2。75

*    乘数                    3*5=15

-    减法                    5-3=2

+    加法                   3+2=5

优先级:

**操作符首先求值

接下来是-*、/、//和%操作符,从左到右。

+和-操作符最后求值,也是从左到右。

如果需要,可以用括号来改变通常的优先级。

数据类型:

整型、浮点型、字符串(单引号)

字符串连接:+(只能连接2个字符串)

'hello'+'world'------>'helloworld'

字符串复制:*

’Alice‘*5------->'AliceAliceAliceAliceAlice

变量:赋值语句a=2

变量名规则:只能是一个词

只能包含数字、字符和下划线

不能以数字开头

变量名区分大小写,Python一般以小写开头

一个小程序:

#This program says hello and ask for my name

print('hello world!')

print('What is your name?')

#ask for their name

myName = input()

print('It is good to meet you,'+myName)

print('The length of your name is:')

#ask for their age

print('What is your age?')

myAge=input()

print('You will be '+str(int(myAge)+1)+' in a year')

#注释行

print()         函数——将括号内的字符串显示在屏幕上可以使用print()打印出空行

myName = input()    用户键盘端输入的字符串赋值给myName这个变量

input()         返回的是字符串

len()            求字符串的长度(整型)

str()             将其他数据类型转换成字符串形式

int()             将其他数据类型转换成整型

float()         将其他数据类型转换成浮点型

round(flt, ndig=0)  其中 ndig 是小数点的后面几位,然后对原浮点数 进行四舍五入的操作。

round(3.1415926,2)--------->3.14

上一篇 下一篇

猜你喜欢

热点阅读