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

Python learning notes_ Day02

編輯:Python

Judge

  • Data type can also be used as a judgment condition . Any value is 0 All of the figures show that False, Not 0 Express True; Any non empty object represents True, The empty object is False.
  • Conditional expression ```python

a = 10 b = 20 if a <= b: … smaller = a … else: … smaller = b … smaller 10

# Rewrite the above judgment as a conditional expression ( It's also called the ternary operator )

s = a if a <= b else b s 10 ```

Expand if sentence : Multi branch statement , If a certain condition is satisfied, the corresponding statement block will be executed , Other conditions are no longer judged . Multiple branches execute only one branch .

Random number module

>>> import random
# random.choice Randomly select an item from a given list
>>> random.choice('abcdef')
'a'
>>> random.choice('abcdef')
'c'
>>> random.choice(['aaa', 'bb', 'cccc', 'ddd'])
'bb'
>>> random.choice(['aaa', 'bb', 'cccc', 'ddd'])
'ddd'
>>> random.choice(['aaa', 'bb', 'cccc', 'ddd'])
'ddd'

while loop

python The middle cycle is divided into while Circulation and for loop , When the number of cycles is unknown , Use while loop , The number of cycles is known , Use for loop .

while The loop condition :
Code groups in the circulatory body

When the loop condition is true , Execute the code group in the body of the loop . The condition is true and if The judgment is the same .

break and continue

  • break: End of cycle , In the loop body break The subsequent code will not execute
  • continue: Skip this cycle , In the loop body continue The subsequent code will not execute

else sentence

Cyclic else sentence : When the loop is break,else Statement does not execute , Otherwise execution

for loop

range function

Used to generate integers .

  • Parameters are given only one number , It means the ending number , The starting number is from... By default 0 Start , The ending number does not contain
>>> range(10) # Generate range object
range(0, 10)
>>> list(range(10)) # Convert to list , For viewing only range Numbers that can be generated
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(10):
... print(i)
  • Give two numbers to the parameter , Start and end numbers , The ending number does not contain
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • range The third parameter of , Is the step value
>>> list(range(1, 11, 2))
[1, 3, 5, 7, 9]
>>> list(range(10, 0, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

List of analytical

A convenient and fast way to generate a list

>>> [10]
[10]
>>> [10 + 2] # The result of the expression evaluation is put in the list
[12]
>>> [10 + 2 for i in range(5)] # Loop determines how many times the expression is evaluated
[12, 12, 12, 12, 12]
>>> [10 + i for i in range(1, 11)] # The expression can be a loop variable
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> [10 + i for i in range(1, 11) if i % 2 == 1] # If the judgment condition is true, the calculation result will be retained
[11, 13, 15, 17, 19]
# Equivalent to the following code :
>>> nums = []
>>> for i in range(1, 11):
... if i % 2 == 1:
... nums.append(10 + i)
...
>>> nums
[11, 13, 15, 17, 19]

Generated by list parsing 192.168.1.0/24 All of the segments IP Address :

>>> ['192.168.1.%s' % i for i in range(1, 255)]

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