1 acquaintance Python3

Acquaintance Python3
a, Python3 programming the first step
in a previous tutorial we have learned the basic grammar of some Python3, let's try to write a Fibonacci sequence.
Example (Python 3.0+)

! Fibonacci sequence

The sum of the two elements determines the next number

a, b = 0, 1
while b < 10:
print(b) a, b = b, a+b

Wherein the codes a, b = b, a + b is calculated for the first calculation expression on the right, and at the same time assigned to the left, equivalent to:
n-B =
m = A + B
A = n-
B = m
or more execution program, output results:
. 1
. 1
2
. 3
. 5
. 8
this example describes several new features.
The first line contains a compound assignment: the variables a and b simultaneously get the new values 0 and 1. The last line again used the same method, you can see the expression on the right will be executed before the assignment change. The execution order of expression on the right is from left to right.
Output variable values:

256 * 256 = I
Print ( 'I values:', I)
I values: 65536
end keywords
keyword end may be used to output to the same line, or add different characters output at the end of the Examples as follows:
example (Python 3.0+)
fibonacci

The sum of the two elements determines the next number

a, b = 0, 1
while b < 1000:
print(b, end=',')
a, b = b, a+b

Performing the above procedure, output is:
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

Two, Python3 conditional control
Python via one or more conditional statements are statements of the execution result (True or False) to determine the execution code block.
We can understand the simple execution of conditional statements by the following figure:

Code execution:
if statement
general form Python if statement is as follows:
if condition_1: statement_block_1 elif condition_2: statement_block_2 the else: statement_block_3
• If the "condition_1" will be performed to True "statement_block_1" block statement
• If the "condition_1" is False, the judge "condition_2"
• If "condition_2" to True will perform "statement_block_2" block statements
• If "condition_2" to False, will perform "statement_block_3" block statements
Python elif instead of using else if, and so the keywords if statement is : if - elif - else.
Note:
• 1, behind each condition you want to use a colon: indicates followed by a statement block after satisfying the conditions to be executed.
• 2, indentation divided statement blocks, the number of statements in the same indentation together to form a block.
• 3, there is no switch in Python - case statement.
The following is a simple if Example:
Example 1

!/usr/bin/python3

= 100 var1
IF var1:
Print ( ". 1 - IF condition expression to true")
Print (var1)
var2 = 0
IF var2:
Print ( "2 - IF condition expression to true")
Print (var2)
Print ( "Good bye! ")

Execution code above, the output is:
. 1 - if the conditions for expression to true
100
! BYE Good
From the results can be seen due to the variable var2 is 0, if the statement is not within the corresponding execution.
The following examples demonstrate the calculation to determine the age of the dog:
Example 2

!/usr/bin/python3

age = int (input ( "Please enter your family dog's age:"))
Print ( "")
IF Age <= 0:
Print ( "! You're kidding me")
elif Age == 1:
Print ( . "equivalent to 14 years of age")
elif Age == 2:
Print (. "equivalent to 22 years of age")
elif Age> 2:
human = 22 + (Age -2). 5 *
Print ( "a corresponding human Age : ", human)

Quit Tips

input ( "click enter button to exit")
to save the above script in dog.py file, and execute the script:
$ python3 dog.py
Please enter your house dog's age: 1

Equivalent to 14 years of age.
Hit enter to exit
the following common operations as if the operator:
Operator Description
<Less than
<= Less than or equal

Greater than
= greater than or equal to
== Equal to compare two values are equal
! = Not equal to
Example 3

!/usr/bin/python3

Program demonstrates the == operator

Using digital

print(5 == 6)

Using Variables

x = 5
y = 8
print (x == y)

The above examples output:
False
False
high_low.py file demonstrates the comparison operation numbers:
Examples

!/usr/bin/python3

This example demonstrates the digital guessing game

7 = Number The
GUESS = -1
Print ( "Digital guessing game!")
the while GUESS = Number The:!
GUESS = int (the INPUT ( "Please enter the number you guess:"))

if guess == number:
    print("恭喜,你猜对了!")
elif guess < number:
    print("猜的数字小了...")
elif guess > number:
    print("猜的数字大了...")

The implementation of the above script, examples of output results are as follows:
$ python3 high_low.py
! Digital guessing game
Enter the number you guess: 1
digital guess a little ...
Please enter the number you guessed: 9
guess is big ...
Please enter the number you guessed: 7
Congratulations, you guessed it!
________________________________________
if nested
in nested if statements, can if ... elif ... else in another configuration if ... elif ... else structure.
1 if expression:
statement
if expression 2:
Statement
elif Expression 3:
Statement
else:
Statement
elif Expression 4:
Statement
else:
Statement
Example

!/usr/bin/python3

num = int (input ( "Enter a number:"))
IF NUM% 2 == 0:
IF NUM == 0. 3%:
Print ( "the number you entered is divisible by 2 and. 3")
the else:
Print ( "you the number can be divisible by 2, but not divisible by 3 ")
the else:
IF NUM% 3 == 0:
Print (" the number you enter is divisible by 3, but not divisible by 2 ")
the else:
Print (" the number you entered can not be divisible by 2 and 3 ")

Save the above program to test_if.py file, after executing output is:
$ python3 test.py
Enter a number: 6
number you enter is divisible by 2 and 3

The following Examples 0-99 and x is a number taken, y is a number of 0-199 to take, if x> y x is output, then the output y if x is equal to x + y, y else output.

!/usr/bin/python3

import random

x = random.choice(range(100))
y = random.choice(range(200))
if x > y:
print('x:',x)
elif x == y:
print('x+y:', x + y)
else:
print('y:',y)

!/usr/bin/python3

"" "The above example of an extended" ""

print ( "======= Welcome to the age of the dog contrast system ========")
the while True:
the try:
Age = int (the INPUT ( "Please enter the age of your home dog:"))
Print ( "")
Age = float (Age)
IF Age <0:
Print ( "? You kidding me")
elif Age == 1:
Print ( "the human equivalent of 14 years old")
BREAK
elif Age == 2:
Print ( "the human equivalent of 22 years old")
BREAK
the else:
human = 22 + (Age - 2) * 5
Print ( "the human equivalent:", human)
BREAK
the except ValueError:
Print ( "input is not legitimate, please enter a valid Age" )

Quit Tips

input ( "click enter button to exit")

Puzzle games optimized digital
print ( 'Second, digital guessing game')
print ( 'digital guessing game!')

. 1 = A
I 0 =
the while A = 20 is:!
A = int (INPUT ( 'Enter your guess numbers:'))
I + =. 1
IF A == 20 is:
IF I <. 3:
Print ( 'really serious, so soon guessed it! ')
the else:
Print (' finally guessed it, Congratulations')!
elif a <20:
Print ( '! your guess is small, do not lose heart, continue to work hard')
the else:
Print ( 'your guess is big, do not lose heart, continue to fuel!')

!/usr/bin/python3

Continues to expand, adding a user prompt to determine whether to exit or continue

print ( "======= Welcome to the age of the dog contrast system ========")
Control = "N"
the while Control == "N":
the try:
Age = int (the INPUT ( "Please enter your family dog's age: "))
#Print (" ")
Age = float (Age)
IF Age <0:
Print ("? You kidding me ")
elif Age == 1:
Print (" the human equivalent of 14 years old ")
#break
elif Age == 2:
Print (" the human equivalent of 22 years old ")
#break
the else:
human = 22 + (Age - 2) * 5
Print (" the human equivalent: ", human)
#break
the except ValueError:
Print ( "input is not legitimate, please enter a valid Age")
Print ( "")
Control the iNPUT = ( "exit (the Y-/ N)?")
Print ( "")

Quit Tips

input ( "click enter button to exit")

Use judgment statement to achieve the calculation of BMI.
BMI index (ie, body mass index, BMI for short, also known as body weight, English as Body Mass Index, BMI for short), is divided by the square of the number of digital meters in height with a draw weight in kilograms

!/usr/bin/env python3

print ( '---- Welcome BMI calculation program ----')
name = the INPUT ( 'Type your name:')
height = eval (the INPUT ( 'Please type in your height (m):'))
weight = eval (input ( 'Please type your body weight (kg):'))
gender = the INPUT ( 'type your gender (F / M)')
BMI = float (float (weight) / (float (height) **2))

official

BMI IF <= 18.4:
Print ( 'Name:', name, 'physical state: lean')
elif BMI <= 23.9:
Print ( 'Name:', name, 'physical condition: Normal')
elif BMI <27.9 = :
Print ( 'name:', name, 'physical state: overweight')
elif BMI> = 28:
Print ( 'name:', name, 'physical condition: obesity')
Import Time;

time module

= nowtime (time.asctime (time.localtime (time.time ())))
IF Gender == 'F':
Print ( 'thanks', name,' lady ', nowtime,' the use of this program, I wish you good ! health ')
IF Gender ==' M ':
Print (' thanks', name, 'Mr.', nowtime, 'the use of this program, I wish you good health')!

N query to all prime numbers between m

def find_prime_number(n, m):
if isinstance(n, int) and isinstance(m, int):
if m <= 1:
return "error range"
if 1 >= n > m:
return "error start"
numbers = list()
num = n
while num <= m:
i = 2
while i < num:
if (num % i == 0) and (num != i):
break
else:
i += 1
if num == i:
numbers.append(num)
num += 1
return numbers
else:
return "error input"
print(find_prime_number(1, 100))

Taking the random number expansion. Taking the random number until the number is equal to two, the number of display access time.
Random Import
X = The random.choice (Range (100))
Y = The random.choice (Range (100))
B, X = C, Y
A. 1 =
Print (X, Y)
the while X = Y:!
IF X> Y :
Print ( 'X:', X)
elif X == Y:
Print ( 'Y + X:', X + Y, 'totall CAL', A, 'Times')
the else:
Print ( 'Y:', Y )
X = The random.choice (Range (100))
Y = The random.choice (Range (100))
A = A +. 1
Print ( 'a initialized Data:', B, C, 'Y + X:', X + Y , 'total cal', a, 'times')

Guess you like

Origin www.cnblogs.com/codezeng/p/12532887.html