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


Posted in Python onApril 26, 2019

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

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

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

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

2、运算符

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

#!/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.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解

注: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、字符串基本运算符

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

代码举例:

#!/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条件语句

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

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

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

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

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

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

示例代码:

#!/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

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

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

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

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

示例代码:

#while循环计算1-100的和
a = 1
sum1 = 0
while a<=100:
  sum1 += a
  a += 1
print(sum1)

运行结果:

5050

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

示例代码:

#while循环嵌套
i = 1
while i<=5:		#控制行数
  j = 1
  while j<=i:		#控制*的个数
    print("*",end="")
    j+=1
  i+=1
  print()

运行结果:

*
**
***
****
*****

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

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

#让用户控制循环条件

i = True
while i:
  inpu = input("是否退出程序?(y/n):")
  if inpu == "y":
    i = False

运行结果:

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

(3)循环语句——for

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

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

(4)for循环应用

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

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

注: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行等腰三角形

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

#!/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语句及应用

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

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

#!/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语句及应用

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

Python3.5基础之变量、数据结构、条件和循环语句、break与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语句

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

(7)range()函数

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

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

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

6、Python数据结构

(1)list——列表

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

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

#!/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)元组

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

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

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

# 元组
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)字典

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

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

#字典(无序--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单例模式实例分析
Apr 08 Python
python django事务transaction源码分析详解
Mar 17 Python
Python实现类的创建与使用方法示例
Jul 25 Python
Python callable()函数用法实例分析
Mar 17 Python
使用pandas中的DataFrame数据绘制柱状图的方法
Apr 10 Python
python如何制作英文字典
Jun 25 Python
python线程中的同步问题及解决方法
Aug 29 Python
python读写Excel表格的实例代码(简单实用)
Dec 19 Python
python爬虫把url链接编码成gbk2312格式过程解析
Jun 08 Python
python实现简单的井字棋游戏(gui界面)
Jan 22 Python
python3+PyQt5+Qt Designer实现界面可视化
Jun 10 Python
python套接字socket通信
Apr 01 Python
python实现名片管理系统项目
Apr 26 #Python
python面向对象实现名片管理系统文件版
Apr 26 #Python
Python判断对象是否为文件对象(file object)的三种方法示例
Apr 26 #Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
Apr 26 #Python
浅谈python新式类和旧式类区别
Apr 26 #Python
Python静态类型检查新工具之pyright 使用指南
Apr 26 #Python
Python3中_(下划线)和__(双下划线)的用途和区别
Apr 26 #Python
You might like
令PHP初学者头疼十四条问题大总结
2008/11/12 PHP
PHP 单引号与双引号的区别
2009/11/24 PHP
简单的pgsql pdo php操作类实现代码
2016/08/25 PHP
PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
2019/02/15 PHP
JavaScript 仿关机效果的图片层
2008/12/26 Javascript
javascript跨域的4种方法和原理详解
2014/04/08 Javascript
AngularJS入门教程之控制器详解
2016/07/27 Javascript
Node.js和Express简单入门介绍
2017/03/24 Javascript
详解Vue 事件驱动和依赖追踪
2017/04/22 Javascript
mui 打开新窗口的方式总结及注意事项
2017/08/20 Javascript
Angular2监听页面大小变化的解决方法
2017/10/09 Javascript
jQuery实现切换隐藏与显示同时切换图标功能
2017/10/29 jQuery
JavaScript实现构造json数组的方法分析
2018/08/17 Javascript
浅谈vue加载优化策略
2019/03/19 Javascript
详解微信小程序开发用户授权登陆
2019/04/24 Javascript
vue中根据时间戳判断对应的时间(今天 昨天 前天)
2019/12/20 Javascript
JS中锚点链接点击平滑滚动并自由调整到顶部位置
2021/02/06 Javascript
Python 字符串中的字符倒转
2008/09/06 Python
python修改操作系统时间的方法
2015/05/18 Python
Python实现读取txt文件并画三维图简单代码示例
2017/12/09 Python
python+numpy实现的基本矩阵操作示例
2019/07/19 Python
python dumps和loads区别详解
2020/02/04 Python
python 穷举指定长度的密码例子
2020/04/02 Python
解析Python 偏函数用法全方位实现
2020/06/26 Python
详解python安装matplotlib库三种失败情况
2020/07/28 Python
Python return语句如何实现结果返回调用
2020/10/15 Python
python中scipy.stats产生随机数实例讲解
2021/02/19 Python
会走动的图形html5时钟示例
2014/04/27 HTML / CSS
珠宝店促销方案
2014/03/21 职场文书
内蒙古鄂尔多斯市市长寄语
2014/04/10 职场文书
欢迎领导检查标语
2014/06/27 职场文书
违反纪律检讨书范文
2015/05/07 职场文书
小马王观后感
2015/06/11 职场文书
感恩父母主题班会
2015/08/12 职场文书
七个Python必备的GUI库
2021/04/27 Python
如何判断pytorch是否支持GPU加速
2021/06/01 Python