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

Python - type, operation and process control (day01, 02)

編輯:Python

Catalog

One 、 Basic knowledge of

1.1 The outline

1.2 Input and output

  Two 、 Process control

2.1 Sequential process

2.2 Choose the process / Branch process

2.3  Nested condition selection

2.4  Circulation process


One 、 Basic knowledge of

1.1 The outline

print('hello world')
print(' Life is too short , Enjoy pleasure in good time ')
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: Indent right

shift+tab: Indent left

ctrl+/ Multiple lines of direct comments

print Back plus ,end=''  Don't wrap ,end' ' Add a space at the end

range(1,10):1~9

1.2 Input and output

String formatting method (% by : Place holder , Followed by variable types ):

# Output % Place holder
me=' my '
classPro=' One year at Tsinghua high school 3 class '
age=7
print('%s The name is Xiao Ming : come from 【%s】 This year, %d Year old '%(me,classPro,age))
print('%s The name is fat tiger : come from 【%s】 This year, %d Year old '%(me,classPro,age))
print('%s The name is Tinker Bell : come from 【%s】 This year, %d Year old '%(me,classPro,age))
print(' I can \n A new line ') #\n Line feed effect
# Practice output
# full name : The professor
# QQ:66666666
# cell-phone number :5024193635
# The company address : Baiyun District, Guangzhou 1
#
# name=' The professor '
# QQ='66666666'
# phone='5024193635'
# addr=' Baiyun District, Guangzhou 1'
# print(' full name :{} Age is :{} year '.format(name,23))
# print('QQ:{}'.format(QQ))
# print(' cell-phone number :{}'.format(phone))
# print(' Address :{}'.format(addr))
# print('------------ Above is format Formal -------------------')
# print(" full name :%s"%name)
# print("QQ:%s"%QQ)
# print(" cell-phone number :%s"%phone)
# print(" Address :%s"%addr)
# Other ways of format output .format()
# input The practice of Get the content entered by the keyboard
# name=input(" Please enter your name :")
# age=int(input(" Please enter your age :"))
# print(type(name))
# QQ=input(' Please enter your qq')
# phone=input(" Please enter your phone number :")
# addr=input(" Please enter your address :")
#
# print(' full name :%s Age is :%d year '%(name,age))
# print(' full name :{} Age is :{} year '.format(name,age))
# print('QQ:{}'.format(QQ))
# print(' cell-phone number :{}'.format(phone))
# print(' Address :{}'.format(addr))

input The received keyboard input is str type , If it's a number , To translate into int type

  Two 、 Process control

Classification of process control :

2.1 Sequential process

    Code is a top-down execution structure , It's also python The default process

2.2 Choose the process / Branch process

    According to the judgment at a certain step , A structure that selectively executes the corresponding logic
(1) Single branch  
               if Conditional expression :
                    One by one python Code               
                   .......
(2) Double branch
               if Conditional expression :
                    One by one python Code
                   .......
               else:
                    One by one python Code
                   .......
(3) Multiple branches
            if Conditional expression :
                    One by one python Code
                   .......
               elif Conditional expression :
                    One by one python Code
               elif   Conditional expression :
                    One by one python Code
                   ....
            Conditional expression : Comparison operator / Logical operators / Compound operator

import random
# score = int(input(' Enter the score :'))
#
# if score < 60:
# print(' fail, ')
# pass
#
# if score >= 60 and score < 90:
# print(' just so so ')
# elif score < 60:
# print(' fail, ')
# else:
# print(' The cow has a big hair ')
count = 1
while count <= 10:
person=int(input(' Please punch :[0: stone 1: scissors 2: cloth ]'))
computer=random.randint(0,2)
if person==0 and computer==1: # Multiple conditions
print(" much .. You win ")
pass
elif person==1 and computer==2:
print(" much .. You win ")
elif person==2 and computer==0:
print(" much .. You win ")
pass
elif person==computer:
print(' Pretty good It was a tie ')
pass
else:
print(' ha-ha ... You lost ')
pass
count+=1

2.3  Nested condition selection

# if-else The nested use of
# A scene needs to be divided into stages or levels , Make a different deal
# To perform internal if sentence Be sure to be external if Only when the statement satisfies the condition can
xuefen=int(input(" Please enter your credits "))
if xuefen>10:
grade = int(input(" Please enter your score "))
if grade>=80:
print(' You can be promoted .. Congratulations ')
pass
else:
print(' unfortunately , Your grades are not up to standard ...')
pass
pass
else:
print(" Your performance is too bad ...")

2.4  Circulation process

    Under certain conditions , The logic of repeatedly executing a piece of code 【 Thing 】

          while Conditional expression :
                    One by one python Code
                   .......
           for ... in   Iteratable collection object :
                    One by one python Code
                   .......


while Use : For unknown number of cycles Used to judge
for Use : For a known number of cycles 【 Iteratable object traversal 】

(1)while

# an isosceles triangle * Print
row = 1
while row <= 10:
j = 1
while j <= 10-row: # Control the number of spaces printed
print('', end='')
j += 1
pass
k = 1
while k <= 2*row-1: # Control printing * Number
print('*', end='')
k += 1
pass
print()
row += 1

(2)for

for data in range(1,101): # The left contains and the right does not contain
sum+=data # Sum up
# print(data,end=' ')
pass
# for----else
account='wyw'
pwd='123'
for i in range(3):
zh=input(' Please enter your account number :')
pd=input(' Please input a password :')
if account==zh and pwd==pd:
print(' Congratulations on your successful login ...')
break # Exit this layer loop
pass
pass
else:
print(' Your account has been locked by the system ...')
# while----else (else Don't execute )
index=1
while index<=10:
print(index)
if index==6:
break
index+=1
pass
else:
print('else Carried out? .....')
# while----else (else perform )
index=1
while index<=10:
print(index)
index+=1
pass
else:
print('else Carried out? .....')
(3)break and continue
# break and continue
# break Represents the end of the interrupt If the conditions are met, the cycle of this layer will be ended directly
# continue: End this cycle , Continue with the next cycle ( When continue When the conditions are met , The remaining statements in this loop will not be executed
# The following cycle continues )
# These two keywords can only be used in a loop 


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