Python Small white Learning path-while, for loop, arithmetic

Source: Internet
Author: User

The loops in Python are basically the same as the loops in other languages, except that the writing format is different.

1. for loop: for [loop condition]:[Loop statement] satisfies the loop condition, executes the loop statement, executes one time judgment once, does not satisfy then ends the loop.

Simple loop: In range () indicates in what range

1 #Cycle Auto-accumulate plus one2 #one parameter represents the starting from 0 to how much3  forIinchRange (10):4     Print("One :", i)5 #two parameters to represent a range6  forIinchRange (0,5):7     Print("Both :", i)8 #three parameters represent the range and execute the statement once in a few loops9  forIinchRange (0,10,2):#2 means a few outputs at a timeTen     Print("thress"I
D:\Python\python.exe D:/pyproject/day1/for.pyone:0one:1one:2one:3one:4one:5one:6one:7one:8one:9two:0two:1two: 2two:3two:4thress 0Thress 2Thress 4Thress 6Thress 8Process finished with exit code 0

2. While statement: while [True/false]: The loop statement condition is true and the loop statement is executed, and the loop is not entered as false.

1Names ="Sunshine"2 #if the conditional return value is true (true), the loop statement is executed3  whilenames = ='Sunshine':4     Print("The condition is true and the name is sunshine. ")5     #exit loop after executing statement6      Break7 8  whilenames = ='Others':9     Print("The condition is true and the name is others. ")Ten     #exit loop after executing statement One      Break
D:\Python\python.exe d:/pyproject/day1/while.py condition is true, the name is sunshine. Process finished with exit code 0

If you do not add a break, because the loop condition is always true, it will always loop and execute the output statement. (Python is limited in general, as if it can be performed up to 998 times).

   Break is the end of the loop, that is, to exit the entire loop, to note that if there are multiple loops nested, break can only exit the current loop. If the break above the speech is replaced with Continuou , the loop will still be executed, and the function of Continuou is to end the cycle and start the next cycle. The statement following the current loop Continuou will not execute.

Ex: after learning the loop statement and the IF condition statement, can be used to write a guess the age of the small program, the requirements are as follows:

(1) Guess the age, guess the age is big, the hint bigger

(2) Guess age, guess the age is small, the hint smaller

(3) Guess right, output age, and congratulations

1 #correct age2Age_of_oidboy = 563  whileTrue:4     #enter guessing age5guess_age = Int (input ("Guess Age:"))6     #guess right.7     ifGuess_age = =Age_of_oidboy:8         Print("Yes,you got it.")9          BreakTen     #big guess. One     elifGuess_age >Age_of_oidboy: A         Print("Think smaller ...") -     #small guess. -     Else: the         Print("Think bigger ...")
View Code

Results:

D:\Python\python.exe d:/pyproject/day1/for.pyguess age:15think bigger...guess age:57think smaller...guess age:56Yes, You got it. Process finished with exit code 0

  


What is an operation?
Arithmetic, mathematically, an operation is a behavior that obtains a new quantity by a possible combination of known quantities. The essence of an operation is the mapping between sets
1. Logical operation (return value is a Boolean type)
Hypothesis: A,b,c = 3,5,7
1.and Ex: a > 2 and C < 7-->flase
2.or Ex: a > 2 or c = 8 -->true
3.not Ex: if not a > 2 and C < 7:print ("ddd") output Result: DDD
1 #Logical Operations2A,b,c = 3,5,73 #and operation two conditions are satisfied to be true4 Print("and :", a > 2 andC < 7)5 #an OR operation satisfies either of these conditions is true6 Print("or:", a > 2orc = = 2)7 #the not operation is not performed if it is not a<2 and c>58 if  notA < 2 andC > 5:9     Print("Successful Execution")

Results:

D:\Python\python.exe D:/pyproject/bogle.pyand:falseor:true Execute successful process finished with exit code 0

  

2. Member Operations  (return value is a Boolean type)
(1) in Operation Judging is not inside
Now there is a list a[1,2,3,4]
In:1 in a means: 1 is a member of List A. Result: True
Not:5 not in a means: 5 is not a member of List A. Result: False
1 #Create a list2A = [1,2,3,4]3 #In operation4 Print("In operation, 1 is a member of a:", 1incha)5 #Not Operation6 Print("not operation, 5 is not a member of a:", 4 not inchA
Results:
D:\Python\python.exe D:/pyproject/bogle.pyin operation, 1 is a member of a: Truenot operation, 5 is not a member of a: Falseprocess finished with exit code 0
(2) is Operation   judgment is not a member of what
a[1,2,3,4]
names = ' Hello world! '
is type (a) is list indicates that the type of a is a list-->true
is isn't type (names) is not str: the type of names is not a string type-->false
1 #Create a list and string2A = [1,2,3,4]3Names ="Hello world!"4 #is operation5 Print("is operation, the type of a is a list:", type (a) islist)6 #is not operation7 Print("is operation, names is not a string type:", type (names) is  notStr

Results:

D:\Python\python.exe D:/pyproject/bogle.pyis operation, the type of a is a list: Trueis operation, names is not a string type: Falseprocess finished with exit code 0

3. Bitwise operation (converted to binary operation)
now there are a,b,c three values for 60,13,0
A = (00111100),
B = (00001101),
C = 0 (00000000)
perform the following operations:
(1) & and operations A & B =00001100-->12 (00001010) (same as 1, true 1)
(2) | or operation a | b =00111101-->61 (00111101) (with one 1, or 1)
(3) ^ xor a ^ b =00110001-->49 (0011001) (same as 0, different 1)
(4) ~ Inverse operation ~ a =1 11000011-->-195 (the front is the symbol bit, will also take the reverse)
(5) << left shift operation = << 2 = 256 64 (00100000) shift left 2 bit: (10000000)
(6) >> right-shift operation: >> 2 = 16 64 (00100000) Right Shift 2-bit (000001000)
1A = 602b = 133c =04 Print("& Operations:", A &b)5 Print("| operation:", a |b)6 Print("^ Arithmetic:", a ^b)7 Print("~ operation:",~a)8 Print(">> Operations:", >> 2)9 Print("<< Operations:", << 2)

Results:

D:\Python\python.exe d:/pyproject/bogle.py& Operation: 12| operation: 61^ operation: 49~ operation: -61>> operation: 16<< operation: 256Process Finished with exit code 0

  

 
The smallest unit that can be represented in a computer, is a bits (bit)
The smallest unit that can be stored in a computer is a bits (bit)
8 bit (bit) = 1 byte (bytes)
The characters in computer I must be at least one byte to represent
1024byte = 1kbyte
1024kbyte = 1mbyte
1024mbyte = 1GByte
1024GB = 1Tb


Ternary operations:
The ternary operator is a fixed format in software programming, and the syntax is "conditional expression?" Expression 1: Expression 2 ". Use this algorithm to filter the data when it is called. (Baidu Encyclopedia Reference)
1. Syntax: Conditional expressions? Expression 1: Expression 2
Description: The position in front of the question mark is the condition to be judged, the result is bool type, true when the expression 1 is called, and the expression 2 is called when false.
The logic is: "If the condition is set up or satisfied then the expression 1 is executed, otherwise the second is executed." ”
2. You can also use if...else ...
a,b,c=1,3,5
D = A If a>b else C (if A>b is d=a, otherwise d=c)-->d=5

Python Small white Learning path-while, for loop, arithmetic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.