Python looping conditions, variables, string formatting

Source: Internet
Author: User

I. Knowledge of Python

Python language advantages and disadvantages, self-Baidu, here does not outline, simply say, Python is an object-oriented, interpreted computer language. So the question is, what is the difference between an explanatory and a compiled language?

1. Explanatory and compiled language differences

interpreted language: when the program runs, it compiles, and the code executes one line of compilation. Compile the source code into a. PYC bytecode, which is a binary file. such as java,python,php, etc.

Like eating hot pot, explaining the type is to eat while eating, this process needs to be cooked, slow

compiled language: before running the code, it must be compiled to compile all the code into a binary file. Once compiled, run multiple times, fast. such as c,c++

It is like eating hot pot, the compilation of the dish is cooked all cooked, one-time eat finished, fast

2. Variables

As the name implies, a variable is a parameter of change, as opposed to a constant. Variables are information that is used to store multiple data types.

#variables in Python do not need to be declared type, determined by value, usage: variable name = variable Valueip='10.1.135.191'#Use single quotation marks, which are character typesAge=18#Enter an integer, int typeheigh=113.2#Enter a decimal, float typelist=[1,2,'Lyh']#define a variable as a list typeDict={name:'Lyh', age:18}#define a variable as a dictionary typeTuple= (a)#define a variable as a tupleName='Liuyihan'#strings can be in single or double quotesName="Let ' s go out!"#when the string itself is enclosed in single quotation marks, the outer layer can only be double quotesName='"Ball Ball" is really annoying! '#The string itself has double quotes, and the outer layer can only be enclosed in single quotes#General multi-line comment with three quotation marks " "#shortcut keys for multiline annotations are ctrl+/Print(Name,age,heigh)#print multiple variables, separated by commas between multiple variables

If you are unsure of the type of the variable, you can view the type of the variable by print (type (variable name)).

3. Cycle Conditions

Loops are repeated things that can be used to iterate and traverse.

3.1 For Loop

The For loop does not need to have its own design number, with its own counter, and is counted by default starting at 0.

 forIinchobjects that can be iterated:#An iterative object has lists, strings, dictionaries, tuples, random numbers, and so on, used to traverseImportRandomnum= Random.randint (1,100)#randomly generated numbersPrint(num)#code is run from top to bottom forIinchRange (3):#Range (3), traversing 0,1,2Guess = input ('Please enter a number:')#the data type entered by input is strguess = Int (guess)#Turn int type    ifGuess>Num:Print('big guess.')    elifguess==Num:Print('Congratulations, you guessed it.')         Break    Else:        Print('small guess.')Else:#all loops are not executed until the normal end    Print('excessive number of errors')

From the last code, you can see the effect of break: the end of the entire loop body, as long as the break, no matter how many times the loop, will immediately end.

3.2 While loop

While is when .... , that is, when the condition is fulfilled, the following code is executed, after execution, then the while this judgment, see if the condition is satisfied, until not satisfied, the end.

While loop, you need to set the counter.

The loop is the code that repeats inside the loop body, count=0 while count<=5:    +=1    if count==3: # the value equals 3 o'clock, perform the following continue        Continue # Encounter it, end this loop, continue after the execution, continue the next cycle    Print (count) Else :     Print (' end ')

So the above code runs the result: 1,2,4,5,6. Did you guess right??

4. String Formatting

There are three main methods:

4.1 + sign Connection
Name=' Blog Park 'print(" welcome to "+name) running result: Welcome to the Blog Park 
4.2% Placeholder

% is the role of pre-occupied pits, first occupy the position, in order to print, the position is wrong, the result will be problematic

Name='liuyihan' Your name is%s your  age is%s your score is%s"% ( name,28,87.3)# here integers can be used with%d, decimals available%f,%.2 to preserve 2-bit decimal print(words) Run Result: your name is Liuyihan.   28 Your score is 87.3.
Example 2:
Print ("Welcome to%s%s"% (' lyh ', ' Blog Park ')
Running result: Welcome to LYH's Blog Park
4.3 format formatted
' your name {name} your age {ages} '. Format (name=' little black ', age=18print18

When the format placeholder specifies a parameter name, regardless of the order, the value is found in the specified parameter name, with the specific value output.

  

Python looping conditions, variables, string formatting

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.