初识python

来源:互联网 发布:mac层随机竞争算法 编辑:程序博客网 时间:2024/06/09 14:40

Python是一门动态解释型的强类型语言。

1.解释型语言和编译型语言。
1.1编译型语言是一次性将全部程序编译成二进制文件,然后在运行。
运行速度很快,但是开发效率低,不能跨平台。
1.2解释型语言是程序执行时将代码逐行解释成机器语言由计算机执行,运行速度相对较慢,但是调试代码时很方便,开发效率高,并且可以跨平台。

python基础一
1.变量
1.1命名规则
python变量是数字,字母,下划线的任意组合,不能以数字开头,
不能是python中的关键字。python中的关键字可以这样查看

>>> import keyword>>> keyword.kwlist

除此之外,python变量还应具有可描述的特点,不能是中文,拼音,不能太长。

name ='MUYI'age = 12

常见命名类型有
驼峰型: ZenOfPython
下划线型:zen_of_python

2.常量
python中没有一个专门的语法代表常量,一般约定俗成变量名全部大写来代表常量。

3.注释
python用#表示单行注释
”’….”’或者”“”…..”“”表示多行注释

4.基本数据类型
4.1数字
*int();运算*
4.2字符串
str()函数
在Python中,加了引号的字符都被认为是字符串。

name = 'MUYI'Job = "student"

字符串的运算(加,乘)

a = 'hello'   b = 'muyi'print(a + b)a = 'spam'b = a*8print(b)

另外还有多行字符串,多行字符串,必须要用多引号。

mes ='''sdsdsdsda,sdsdfsewqq,qwewrqqrwer.'''print(mes)

字符串的格式化
格式化牵涉到占位符,是指通过放置占位符,把字符串里的占位符与外部的变量做个映射关系
通常%默认为就是占位符的%
字符串为%, 数字为%d

name = input("Name:")age = input("Age:")job = input("Job:")hobbie = input("Hobbie:")info = '''------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name Name  : %s  #代表 name Age   : %s  #代表 age  job   : %s  #代表 job Hobbie: %s  #代表 hobbie ------------- end -----------------''' %(name,name,age,job,hobbie)  # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 print(info)

此外,因为%通常默认为占位符,所以通常其后再加一个%,对前面的%做下转译。

msg = "我是%s,年龄%d,目前学习进度为80%%"%('木易',18)print(msg)

4.3布尔值(bool)
布尔值为True和False,常用来用作逻辑判断。

print(‘aaa’)while Trueprint(‘bbb’)

用户交互input()

1,py2中表示为raw_input()。

2,input()函数input接收的所有输入默认都是字符串格式!

username = input('Enter your username:')password =int(input('Enter your password:'))#此处一定要注意转换数据类型

if 语句

1.if 条件:

if 1 > 2:   print('hello')print('muyi')

2.if 条件:
满足条件执行
else:
if条件不满足执行

if 1 > 2:   print('hello')elseprint('muyi')

3.if 条件:
满足条件执行代码
elif 条件:
上面的条件不满足执行
elif 条件:
……………………
elif 条件:
……………………
else:
上面所有的条件不满足执行

score = int(input("输入分数:"))if score > 100:    print("我擦,最高分才100...")elif score >= 90:    print("A")elif score >= 80:    print("B")elif score >= 60:    print("C")elif score >= 40:    print("D")else:    print("太笨了...E")

while循环语句
while语句在python中用作循环。

while True:    print('hello,world')

如上是一个‘死循环’,怎么跳出循环。
1. 改变条件
2. break 语句
下面以打印1-10为例来进行说明。

count = 1flag = Truewhile flag:    print(count)    count += 1    if count == 101:        flag = False
count = 1while count < 101:    print(count)    count += 1
count = 1while True:    print(count)    count += 1    if count == 101:        break

拓展:
使用while循环输入 1 2 3 4 5 6 8 9 10

count = 0while True:    count += 1    if count == 7:            print('')    else:print(count,end = '')    if count == 10:        break
count = 0while count < 10:    count += 1    if count == 7:        print('')    else:print(count)
count = 0while count < 10:    count += 1    if count == 7:  #着重思考下        print('')        continue    else:print(count)

break语句
break是跳出循环体执行循环后面的语句,完全结束一个循环。即break后面的代码不执行。

print('aaa')while True:    print('bbb')    print('ccc')    print('ddd')    break    print('eee')#注意本行print('fff')

continue语句
continue是跳出本次循环,执行下一个循环。

print(111)while True:    print(222)    print(333)    print(444)    continue    print(555)# print(555)和print(666)并不执行。print(666)

while…..else…..
while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句.

n = 0while n < 6:   n += 1   print('loop',n)   #当循环正常执行完后,再执行elseelse:    print('循环正常执行完啦')print('------out of loop------')
n = 0while n < 6:    n += 1    print('loop',n)    if n ==3:       #while循环被中断        breakelse:    print('循环已经执行完啦')print('-----out of loop-----')
原创粉丝点击