Python Learning Note one

Source: Internet
Author: User

A shortcut key to use

CTRL +? Quick Notes

Ctrl+d Quick Copy

Two operators

The first contact requires special note: <= is less than or equal to, >= is greater than equals;! = is not equal to; = = equals

1) and, or use

if  and score<90:print(' good results ')if' man '  or' woman ':print(' sex legal ') )

2)

Count = count+1count+=1= count-1count-=1count= count*1count*=1  = COUNT/2count/=2

3) which to add a colon (:) if else;

To indent 4 spaces after a colon

Python has 3 types:

int integer type string string type float floating-point type

Three: Variables

What do variables do? The simple point is to store something for the back. Defining variables in Python is simple,

An equal sign does not need to specify the data type, directly define the good

Code:

' Jiang ' Print (name)

Rules for defining variables:
Variable name to see the name to know, can not write, blind write to the back of their own can not understand what the variable is dry, do not use pinyin, so very low, not to use Chinese as a variable name, it is 2b programmers do, but Python can be used in Chinese as the variable name!

Variable names can only be any combination of letters, numbers, or underscores

The first character of a variable name cannot be a number

The following keywords cannot be declared as variable names

' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','exec','finally',' for',' from','Global','if','Import','inch',' is','Lambda',' not','or','Pass','Print','Raise','return','Try',' while',' with','yield']

Four, single quotes, double quotes, and three quotation marks (three single quotes) in Python

Code:

" Let ' Go " # inside is a single quote outside with double quotes ' Liu Weichang "very handsome." ' # inside are double quotes outside with single quotes " " Let ' Go Liu Weichang "very handsome" " " # there are double quotes and single quotes, outside with 3 " " Print ( name) print (title) print (conent) " " # Three quotation marks can also be used for multiple lines of comment

V. INPUT and OUTPUT

How does python receive user input, use the input function, use Raw_input in Python2, receive a string, output, the first program has been written using print, code into the following:

Age = input (' Please enter your ages:'## # # # # # # # # # # # # # # # # #  age = Int (age)  # type conversion to int type

Vi. Conditions of Judgment

In Python, the conditional judgment uses if else to judge, multi-branch words using if elif ... else, that is, if it's what happens, or how it's going to be.

Code:

Age =If age<18:print(' underage ')else  :Print(' adult ')

Type conversions, receiving input examples:

Age = input (' Please enter your ages:'# receive input # as long as input value is received, it is a string type The age = Int (age)  # type conversion goes to the int type if age<18:     Print( ' Minors '  Else:     print(' adult ')

Multi-Conditional judgment:

#Multi-condition judgment if elif else andScore = input ('Please enter your score:') score=Int (score)ifScore>=90:    Print('Excellent')elifScore>=75 andScore<90:     Print('Good')elifScore>=60 andScore<75:     Print('Pass')Else:    Print('inferior lattice')

Multi-condition judgment and small exercise

#Generate a number#user enters a number#the number entered is small, prompting him to enter a small#the input of the number is large, prompting large input large#input is correct. You're the one who asked him.#random generation of numbers, referencing random modulesImportRandomnum= Random.randint (1,100)Print('the randomly generated numbers are:', num)#print out randomly generated numbersNew_num = input ('Please enter what number you want to guess:') New_num=Int (new_num)ifNew_num>Num:Print('the input is big.')elifnew_num<Num:Print('the input is small.')Else:    Print('congratulations on your answer.')

Seven, loop (while for)

Loops (iteration, traversal) means repeating one thing.

while (while else)

The while loop is first judged once, and if the condition is met, recycle count = 0 #计数器

Break encounters a break in the loop, immediately ending the loop, regardless of whether the loop has ended

Continue in the loop encountered continue, then jump out of this cycle, continue the next cycle

Code:

ImportRandomnum= Random.randint (1,10) Count= 0#counter whileCount<3: New_num= Input ('Please enter what number you want to guess:') New_num= Int (new_num)#int conversion    ifNew_num>Num:Print('the input is big.')    elifnew_num<Num:Print('the input is small.')    Else:        Print('Congratulations on your answer, the numbers are:', num) Break   #Break in the loop and end the loop immediately, no matter if the loop is overCount = count+1

While else code

#print (' End of loop! ')##while Loop corresponds to an else, the loop executes after the normal end .ImportRandomnum= Random.randint (1,100) Count= 0#counter whileCount<3: New_num= Input ('Please enter what number you want to guess:') New_num=Int (new_num)ifNew_num>Num:Print('big guess.')    elifnew_num<Num:Print('small guess.')    Else:        Print('Congratulations on your answer, the numbers are:', num) Break   #Break in the loop and end the loop immediately, no matter if the loop is overCount = count+1Else:    Print('The number of games run out! ')#loop corresponds to an else when the loop does not execute until after the normal end .

For

For loop must have an iterative object, in order to loop, for example, there is an array, it is worth mentioning that in other languages, for loop need to first define a counter variable, and then from 0 to add, until the counter to reach your preset value, and then stop the loop, The time to fetch the data is also based on the index of the array starting from 0, which is very troublesome, python for loop is very simple, loop is an iterative object in the element, how many elements you have in this object, how many times the loop

Code:

 for  in range (101):     if i%2==0:        print(i)

Python Learning Note one

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.