Python self-learning note 1

Source: Internet
Author: User

Note: Because python2.x and 3.x still have very big difference, so this study uses is python3.x

One, Python installation

I am using the linux7.4 system, the system comes with the Python version is 2.6, need to upgrade to 3.7

1. Download the Python3.7tar package on your own website.

2. Compiling the installation

[[email protected] home]# ls
Python-3.7.0a2.tgz
[Email protected] home]# Tar XF python-3.7.0a2.tgz
[Email protected] home]# CD PYTHON-3.7.0A2
[Email protected] python-3.7.0a2]# Mkdir/usr/local/python3
[Email protected] python-3.7.0a2]#/configure--prefix=/usr/local/python3--enable-optimizations

[[email protected] python-3.7.0a2]# make//This process takes a long time

3. Replace Python

[Email protected] ~]# cd/usr/bin/
[[Email protected] bin]# MV Python{,.bak}

There are Python, python2.7, python2 three files, in fact, are pointing to python2.7, here will be Python backup

[Email protected] bin]# ln-s/usr/local/python3/bin/python3.7/usr/bin/python

[Email protected] bin]# python-v
Python 3.7.0A2

At this point, the Python upgrade is complete.

Second, variable/assignment

1 " xiaoming " 2 name1 = name3print(name,name1)

Be sure to add a single or double quotation mark when assigning a value to a variable, which is equivalent to a variable instead of a string if not quoted.

Third, comments

Single-line Comment: #被注释内容

Multiline Comment: "" "Annotated Content" ""

Iv. user Input

1. Interactive input

 1  #  !/ Usr/bin/env python  2  3  #   cofing:utf-8  4  Span style= "COLOR: #008080" >5  name = input ( " what is your name:   " )  6  7  passwd = input ( " input your passwd:  Span style= "COLOR: #800000" > " )  8  Span style= "COLOR: #008080" >9  print  (NAME,PASSWD) 

Note: The password entered at this time is clear text, if you want ciphertext input need to use the Getpass module.

1 #!/usr/bin/env python2 3 #Cofing:utf-84 5 ImportGetpass6 7Name = input ("What is your name:")8 9passwd = Getpass.getpass ("input your passwd:")Ten  One Print(NAME,PASSWD)

Summary: Input () and user interaction entered both numeric and string, the system is the default string, if you need to convert the string into shaping requires an int ()

passwd = Int (Input ("Enter your password")//passwd is an integer at this time

2. Formatted output

1 #!/usr/bin/env python2 #Coding:utf-83Name = input ("What is your name:")4age = Int (input ("input your Age:"))5Job = input ("input Your job")6 7info =" "-----------------------Info%s---------------------------------8 Name:%s9 Age :%sTen Job:%s One " "%(name,name,age,job) A Print(info)
format Output Method one
1 #!/usr/bin/env python2 #Coding:utf-83Name = input ("What is your name:")4age = Int (input ("input your Age:"))5Job = input ("input Your job")6 7info =" "-----------------------Info {0}---------------------------------8 Name: {0}9 Age : {1}Ten Job: {2} One " ". Format (name,age,job) A Print(info)
format Output Method two

Summary:%s: string%d: integer%f: floating-point number

V. Expression If...else

User Login Verification

1 #Prompt user to enter account number and password2 #Verify user and password, password requires ciphertext input3 #Prompt user or password input error if error4 #If successful, you are prompted to welcome xxx5 #! /usr/bin/env python6 #Coding:utf-87 ImportGetpass8Name ="xiaoming"9passwd ="xm12345"Ten_name = input ("Please enter your name:") One_PASSWD = Getpass.getpass ("Please enter your password:") A if_name = = Name and_PASSWD = =passwd: -     Print("Welcome {0}". Format (_name)) - Else: the     Print("Password or user name error, please re-enter")

Vi. expression for loop

1 # !/usr/bin/env python 2 # Coding:utf-8 3  for  in Rang (4):     print(i)
1  for  in range (5,10):           //indicates starting from 5 to2     print(i)
1  for  in range (1,10,2):        //from 1 to 10, step is 2 (take once every other number)2     print(i)

Seven, while loop (dead Loop)

If the condition is true, it will be recycled, and if you want to terminate the loop, break continue

Break: Jump out of the loop

Continue: Jump out of the current loop and into the next loop

    • Look at the following example, guess the age of the game, up to only 3 opportunities, when the 3 chance did not guess the right after you will be prompted whether to continue, N for exit, any key represents the continuation.
1 #!/usr/bin/env python2 #Coding:utf-83age_xiaoming = 304Count =05  whileCount < 3:6_age = Int (input ("Please enter your age:"))7     if_age = =age_xiaoming:8         Print("Congratulations, the answer is right:")9          BreakTen     elif_age >age_xiaoming: One         Print("wrong, big guess.") A     Else: -         Print("wrong, small guess.") -Count + = 1 the     ifCount = = 3: -Agin_all = input ("this opportunity has run out, do you want to do it again? ") -         ifAgin_all! ='N': -Count = 0

Python self-learning note 1

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.