Python3.5基礎之變量、數據結構、條件和循環語句、break與continue語句實例詳解

這篇文章主要介紹了Python3.5基礎之變量、數據結構、條件和循環語句、break與continue語句,結合實例形式詳細分析Python3.5編程入門相關的變量、數據結構、常用條件與循環語句操作技巧及注意事項,需要的朋友可以參考下

本文實例講述了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程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章