程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python——類型、運算、流程控制(day01、02)

編輯:Python

目錄

一、基礎知識

1.1 大綱

1.2 輸入輸出

 二、流程控制

2.1 順序流程

2.2 選擇流程/分支流程

2.3 嵌套條件選擇

2.4 循環流程


一、基礎知識

1.1 大綱

print('hello world')
print('人生苦短,及時行樂')
b=()
c=[]
d={}
print(type(b))
print(type(c))
print(type(d))
sum=1
print('sum=',sum)
print('sum=%d'%sum)
print('%d * %d = %d'%(i,sum,i*sum))

tab:向右縮進

shift+tab:向左縮進

ctrl+/多行直接注釋

print後面加 ,end=''  不換行,end' '結尾加個空格

range(1,10):1~9

1.2 輸入輸出

字符串格式化方法(%為:占位符,後面跟變量類型):

# 輸出 %占位符
me='我的'
classPro='清華附中一年3班'
age=7
print('%s名字是小明: 來自【%s】 今年%d歲了'%(me,classPro,age))
print('%s名字是胖虎: 來自【%s】 今年%d歲了'%(me,classPro,age))
print('%s名字是小叮當: 來自【%s】 今年%d歲了'%(me,classPro,age))
print('我可以\n換行嗎') #\n換行效果
# 練習輸出
# 姓名: 老夫子
# QQ:66666666
# 手機號:5024193635
# 公司地址:廣州市白雲區1
#
# name='老夫子'
# QQ='66666666'
# phone='5024193635'
# addr='廣州市白雲區1'
# print('姓名:{} 年齡是:{} 歲'.format(name,23))
# print('QQ:{}'.format(QQ))
# print('手機號:{}'.format(phone))
# print('地址:{}'.format(addr))
# print('------------以上是format形式的-------------------')
# print("姓名:%s"%name)
# print("QQ:%s"%QQ)
# print("手機號:%s"%phone)
# print("地址:%s"%addr)
#格式輸出的其他方式 .format()
# input的練習 獲取鍵盤輸入的內容
# name=input("請輸入您的姓名:")
# age=int(input("請輸入您的年齡:"))
# print(type(name))
# QQ=input('請輸入您的qq')
# phone=input("請輸入您的電話:")
# addr=input("請輸入您的地址:")
#
# print('姓名:%s 年齡是:%d 歲'%(name,age))
# print('姓名:{} 年齡是:{} 歲'.format(name,age))
# print('QQ:{}'.format(QQ))
# print('手機號:{}'.format(phone))
# print('地址:{}'.format(addr))

input接收的鍵盤的輸入都是str類型,如果是數字,要轉化為int類型

 二、流程控制

流程控制的分類:

2.1 順序流程

    就是代碼一種自上而下的執行結構,也是python默認的流程

2.2 選擇流程/分支流程

    根據在某一步的判斷,有選擇的去執行相應的邏輯的一種結構
(1)單分支 
               if 條件表達式:
                   一條條的python代碼              
                   .......
(2)雙分支
               if 條件表達式:
                   一條條的python代碼
                   .......
               else:
                   一條條的python代碼
                   .......
(3)多分支
            if 條件表達式:
                   一條條的python代碼
                   .......
               elif 條件表達式:
                   一條條的python代碼
               elif  條件表達式:
                   一條條的python代碼
                   ....
           條件表達式:比較運算符/邏輯運算符/復合的運算符

import random
# score = int(input('輸入分數:'))
#
# if score < 60:
# print('不及格')
# pass
#
# if score >= 60 and score < 90:
# print('還可以')
# elif score < 60:
# print('不及格')
# else:
# print('牛大發了')
count = 1
while count <= 10:
person=int(input('請出拳:[0:石頭 1:剪刀 2:布]'))
computer=random.randint(0,2)
if person==0 and computer==1: #多條件
print("厲害了..你贏了")
pass
elif person==1 and computer==2:
print("厲害了..你贏了")
elif person==2 and computer==0:
print("厲害了..你贏了")
pass
elif person==computer:
print('不錯 居然是平手')
pass
else:
print('哈哈...你輸了吧')
pass
count+=1

2.3 嵌套條件選擇

# if-else 的嵌套使用
# 一個場景需要分階段或者層次,做出不同的處理
# 要執行內部的 if 語句 一定要外部的 if語句滿足條件才可以
xuefen=int(input("請輸入你的學分"))
if xuefen>10:
grade = int(input("請輸入你的成績"))
if grade>=80:
print('你可以升班了..恭喜您')
pass
else:
print('很遺憾,您的成績不達標...')
pass
pass
else:
print("您的表現也太差了吧...")

2.4 循環流程

    在滿足一定的條件下,一直重復的去執行某段代碼的邏輯【事情】

          while 條件表達式:
                   一條條的python代碼
                   .......
           for ... in  可迭代集合對象:
                   一條條的python代碼
                   .......


while使用:適用於對未知的循環次數 用於判斷
for使用:適用於已知的循環次數【可迭代對象遍歷】

(1)while

# 等腰三角形*打印
row = 1
while row <= 10:
j = 1
while j <= 10-row: # 控制打印空格的數量
print('', end='')
j += 1
pass
k = 1
while k <= 2*row-1: # 控制打印*號
print('*', end='')
k += 1
pass
print()
row += 1

(2)for

for data in range(1,101): #左邊包含右邊不包含
sum+=data #求累加和
# print(data,end=' ')
pass
# for----else
account='wyw'
pwd='123'
for i in range(3):
zh=input('請輸入賬號:')
pd=input('請輸入密碼:')
if account==zh and pwd==pd:
print('恭喜您登錄成功...')
break #退出本層循環了
pass
pass
else:
print('您的賬號已經被系統鎖定...')
# while----else (else不執行)
index=1
while index<=10:
print(index)
if index==6:
break
index+=1
pass
else:
print('else執行了嗎.....')
# while----else (else執行)
index=1
while index<=10:
print(index)
index+=1
pass
else:
print('else執行了嗎.....')
(3)break和 continue
# break和continue
# break 代表中斷結束 滿足條件直接的結束本層循環
# continue:結束本次循環,繼續的進行下次循環(當continue的條件滿足的時候,本次循環剩下的語句將不在執行
# 後面的循環繼續)
# 這兩個關鍵字只能用在循環中


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved