温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句的示例分析

Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句的示例分析

发布时间:2021-07-16 13:55:19 来源:亿速云 阅读:139 作者:小新 栏目:开发技术

小编给大家分享一下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

看完了这篇文章,相信你对“Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI