Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解

本文实例讲述了Python3.5变量、数据结构、条件和循环语句、break与continue语句。分享给大家供大家参考,具体如下:

1、变量:即一个容器概念

Python中的变量时一个弱类型,不需要声明,可以直接使用。通过变量设置的值,编译器根据这个值确定变量的类型。


2、运算符


#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

print(2**3) #幂指数

print(5%3) #取模

print(10&11) #按位与

print(10|11) #按位或

print(10^11) #按位异或

if 1: #1等价于True(非零都等价于False)

print("hello")

else:

print("world")

if 0: #0等价于False

print("hello")

else:

print("world")

运行结果:

8

2

10

11

1

hello

world

3、基本数据类型

注:Python3.x里面,没有long类型,整数都是int类型。

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

i = 888888888888888888

j = 18

k = 0.5689

z = False

s = "hello world"

print(type(i))

print(type(j))

print(type(k))

print(type(z))

print(type(s))

运行结果:

<class 'int'>

<class 'int'>

<class 'float'>

<class 'bool'>

<class 'str'>

4、字符串基本运算符

代码举例:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

print("hello"+"3") #字符串连接

print("hello"*3) #重复输出字符串

a = "abdcjfgg"

print(a[0]) #字符串索引取字符(取第一个字符)

print(a[-1]) #取最后一个字符

print(a[2:4]) #取第三、第四个字符,左开右闭

print(a[2:]) #获取索引值2以及后边的字符

print(a[:2]) #获取索引值小于2的字符

运行结果:

hello3

hellohellohello

a

g

dc

dcjfgg

ab

5、语句——条件和循环

(1)if条件语句

示例代码:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

i = 10

j = 20

if i<15:

print("hello")

if i>15:

print("hello")

else:

print("world")

if i<5:

print("hello")

elif j>12:

print("abc")

else:

print("world")

运行结果:

hello

world

abc

(2)循环语句——while





示例代码:

#while循环计算1-100的和

a = 1

sum1 = 0

while a<=100:

sum1 += a

a += 1

print(sum1)

运行结果:

5050


示例代码:

#while循环嵌套

i = 1

while i<=5: #控制行数

j = 1

while j<=i: #控制*的个数

print("*",end="")

j+=1

i+=1

print()

运行结果:

*

**

***

****

*****



#让用户控制循环条件

i = True

while i:

inpu = input("是否退出程序?(y/n):")

if inpu == "y":

i = False

运行结果:

是否退出程序?(y/n):n

是否退出程序?(y/n):y

(3)循环语句——for



(4)for循环应用

a、利用for循环打印3行直角三角形


注:Python 2.x下的print语句在输出字符串之后会默认换行,如果不希望换行,只要在语句最后加一个“,”即可.

对Python 3.x的print语句:end赋值:print(something, something,.., end=''),使end值为空,这个换行就消除了.


#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

for i in range(3):

for j in range(i*2+1):

print("*",end="")

print("") #打印换行

运行结果:

*

***

*****

b、利用for循环打印3行等腰三角形


#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

#打印3行等腰三角形

for i in range(3):

for j in range(2-i):

print(" ",end="") #空格打印

for k in range(2*i+1):

print("*",end="") #*个数打印

print("") #打印空格

运行结果:

  *

 ***

*****

(5)break、continue语句

a、break语句及应用


#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

#break:从一个循环中直接中断退出

for i in range(5):

if i == 3:

break

print(i)

运行结果:

0

1

2

b、continue语句及应用



#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

#continue:终止当前循环,进入下一次循环

for j in range(5):

if j == 3 :

continue

print(j)

运行结果:

0

1

2

4

(6)pass语句

(7)range()函数




6、Python数据结构

(1)list——列表



#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:ZhengzhengLiu

list = [1,2,3,"hello",1,1]

list.append("world") #列表添加元素

print(list)

print(list.count(1)) #统计列表元素的个数

list.remove(1) #列表删除元素

print(list)

print(list[2:4]) #列表索引查询

list[0] = "hi" #列表修改元素

print(list)

list.reverse() #列表元素反转

print(list)

for i in list: #列表循环查询

print(i," ",end="")

运行结果:

[1, 2, 3, 'hello', 1, 1, 'world']

3

[2, 3, 'hello', 1, 1, 'world']

['hello', 1]

['hi', 3, 'hello', 1, 1, 'world']

['world', 1, 1, 'hello', 3, 'hi']

world  1  1  hello  3  hi

(2)元组


注:元组的元素内容不可变的,一旦改变就变成另外一个对象了,开发中希望用的对象是统一对象,每个对象都有自己的特征和行为,这一点在开发中是非常重要的。


# 元组

tup = (1, 2, 3, "hello")

print(tup[1])

print(tup[0:2])

print(tup.count(1))

for i in tup:

print(i,"",end="")

运行结果:

2

(1, 2)

1

1 2 3 hello

(3)字典




#字典(无序--hash存储)

dic = {"name":"liu","age":18}

print(len(dic)) #打印字典长度

print(dic.get("name")) #根据可以获取值

print(dic.keys()) #打印所有key组成列表

print(dic.values()) #打印所有值组成列表

for i in dic:

print(i) #打印key

for i in dic:

print(dic[i]) #打印值

dic.clear() #清空字典

print(dic)

运行结果:

2

liu

dict_keys(['name', 'age'])

dict_values(['liu', 18])

name

age

liu

18

{}

(4)集合:将重复的元素去掉,用{}

#集合

arry = {1,2,4,2,1,"hello",1,4}

print(arry)

arry.add("bai") #添加元素

print(arry)

arry.remove(2) #删除集合里面元素

print(arry)

for i in arry: #循环打印集合的元素

print(i)

运行结果:

{1, 2, 'hello', 4}

{1, 2, 'hello', 4, 'bai'}

{1, 'hello', 4, 'bai'}

1

hello

4

bai

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python入门与进阶经典教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

以上是 Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解 的全部内容, 来源链接: utcz.com/z/336364.html

回到顶部