Self-Learning Python (i)

Source: Internet
Author: User

First, Python introduction

The founder of Python is Guido van Rossum (Guido van Rossum). During the Christmas of 1989, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.

Python is an explanatory language, a dynamic language, and a strongly typed definition language.

Python: Simple, efficient, portable, extensible, embeddable; The drawback is that code cannot be encrypted, runs slower than C, and threads cannot take advantage of multiple CPUs

Python interpreters are: CPython, IPython, PyPy, Jython, IronPython

Second, the first procedure: Hello world!

Create a hello.py document, enter

1 Print ("Hello world! ")

Third, variable

Note: If you have Chinese in python2.x, you must have it at the beginning of the py file.

1 # -*-coding:utf-8-*-

It is not needed in python3.x.

Examples of variables:

1 " Python_hang " 2 Print (name)

Rules for variable definitions:

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

Variable name the first character cannot be a number

Special keywords cannot be declared as variable names, such as:

[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

Iv. notes for Python

The comments in Python are:

Single-line Comment: #

Multiple lines of comment with: "' annotated content ' or '" ' "Annotated Content" "

Five, the user's input

Python can interact with the user, and the code is:

python2.x with Raw_input (' Please enter: ') such as:

1 name = Raw_input (' Please enter name:')2print name

python3.x with input (' Please enter: ') such as:

1 name = input (' Please enter name:')2print(name)

Note that input is typed in a string type, which requires casting if additional formatting is required

You should use the Getpass method in the Getpass module when you need to encrypt the input password, such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 ImportGetpass5 6 #assign a user-entered password to password encrypt the input with the Getpass method7 8Password = Getpass.getpass ('Please enter your password:')9 Ten #Print Password Contents One  A Print(password)

Vi. concatenation of strings and formatted output

The first type: splicing with +

1Name = input ('Name:')2Age = Input ('Age :')3Job = input ('Job:')4Salary = input ('Salary:')5 6info =" "7 ------Info of" "+ name +" "------8 Name:" "+ name +" "9 Age :" "+ Age +" "Ten Job:" "+ Job +" " One Salary:" "+Salary A  - Print(info)

The second kind: with% splicing

1Name = input ('Name:')2Age = Input ('Age :')3Job = input ('Job:')4Salary = input ('Salary:')5 6Info2 =" "7 ------Info2 of%s------8 name:%s9 age:%sTen job:%s One salary:%s A " "%(name,name,age,job,salary) -  - Print(Info2)

Note: The string is%s, the integer is%d, and the floating-point number is%f

The third type: with {}

1Name = input ('Name:')2Age = Input ('Age :')3Job = input ('Job:')4Salary = input ('Salary:')5 6Info3 =" "7 ------Info3 of {_name}------8 Name:{_name}9 Age:{_age}Ten Job:{_job} One Salary:{_salary} A " ". Format (_name = Name,_age = Age,_job = Job,_salary =salary) -  - Print(INFO3)

Personal feeling the third kind of splicing is practical, convenient and clear.

Seven, If...else ...

1 " "2 user input user name and password, verify correct, correct output "welcome xx", incorrect output "Sorry, user name or password input error!" "3 " "4 5 ImportGetpass6 7Username = input ('Username:')8Password = Getpass.getpass ('Password:')9 Ten #use the IF statement to determine One ifUsername = ='Python_hang'  andPassword = ='123456': A     Print('Welcome', username) - Else: -     Print('Sorry, user name or password input error! ')

Note: The Getpass method cannot be run in Pycharm

Eight, while loop

1 # a simple while loop 2 # if you loop to 100 times, Stop the loop and break out of the loop 3 4 count = 05 while True:6     print( Count)7     count + = 18     if count = =:9          Break

Nine, for Loop

1 # Simple for Loop 2 # Assigning a variable i to a loop using range syntax 3 4  for  in range:5     print('loop', i)

Attached: Range Syntax usage: Range (start value, end value, step value)

Thanks to the old boys education Oldboy, I was watching the old boy education videos in self-study.

Self-Learning Python (i)

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.