Recently idle to have nothing to do, engage in the PY deal!
As a fast-rising language, python programming is really easy to use, which is not to say that he can replace other languages, which is impossible for all languages at present;
Python has his advantages:
Simple, concise, easy to understand, quick to get started;
Have learned Python should have experience, python than many other languages are more portable, easy to write;
Like Python's Hello World:
#!/usr/bin/python
Print (' Hello World ')
Just this one sentence is OK;
For a simple understanding of Python, let's look at the difference between Python2 and 3 below.
Python2 version has been released for a long time, is now widely used, even some of the banking system is written in Python, then he and python3 what is the difference?
1.python2 does not support Chinese input, if there is Chinese, need to add #-*-utf-8-*-at the beginning;
2.python3 directly supports Chinese input;
3.python2 read user input format is: Raw_input (), Python3 bit input ();
Actually said 2 and 3 version of the difference is not too big, Python3 has been infinitely close to compatible Python2 version, in the near future, we can certainly use more convenient, more humane Python3 full version!
Here is a simple study of Python!
The simplest python:
Name = "Qiuye"
Age = "22"
Print (Name,age)
Comments:
Single-line Comment: # cannot exceed 80 characters per line;
Multi-line Comment: ' comment content '
The comments in Python use # and ' ' two characters;
#用来做单行注释, for example: #this is a test
"To do a long comment, such as:
‘‘‘
This
Is
A
Test
‘‘‘
The above is the Python annotation method, simple!
Define variables:
The name of the variable in Python can not be used [email protected]¥% and other similar special characters, you can use the case, with _ line to do variable name, such as:
Usr_name,usrname;
Knowing the above things, we can do some simple examples of practice!
There are several functions you need to know before practicing:
User_input=input ("User input:")----> Read user input, string;
User_input=int (Input ("User input:"))----> Converts the input information into a numeric output ;
Example: Print out what the user has entered:
User_input=input ("User input:")
Print ("User input content is:", user_input)
Python format:
Python has strict programming format requirements, each line must start at shelf, otherwise judgment error, if statement, for Loop statement, etc., the child line needs to start empty 4 blank characters, such as
If user = Username:
Print ("Test")
Else
Print ("End")
The indent character defaults to 4 characters, one tab character;
Format string:
The above a simple example should not be difficult to understand, the following is the format of the string :
In order to format the information output, beautiful, we have the format of the string this thing;
Formatting symbols:%s a symbol represents a variable value %d represents a value %f represents a numeric value with a decimal point
Like what:
Name = () Age = () job = () msg =% (name,name,age,job) (msg)
Expression If...else:
Logic to determine the input information, to output.
Force indentation language.
Cases:
user = ' Qiuye '
passwd = 123456
Username = input ("User:")
Password = input ("passwd:")
If user = = Username: #进行判断;
Print ("username is correct ... ") #如果判断为true则输出, and continue;
if password = = passwd:
Print ("passwd is correct ... ")
else: #如果判断为false, then output, and return an error;
Print ("Password in invalid ...")
Else
Print ("Username or password is incorrect ...")
Multi-condition simultaneous judgment :
If user = = Username and passwd = = password:
Print ("Welcome login")
Else
Print ("Username or password is incorrect ...")
Multi-Branch judgment:
Example: guessing numbers
Age = 22
guess_num = Int (input ("Input your guess num:"))
if Guess_num = = Age:
Print ("congratulations!")
Elif guess_num > Age: #如果上面判断不成立, then continue to judge, if the judgment is not established then continue;
Print ("num is larger!")
Else
Print ("num is smaller!")
Cycle:
Pass placeholder, does not affect the program to run, continue to execute the statement;
Range () defines the number of cycles, such as:
For I in range (10):
Print (i)
Break, define the loop exit condition
Example: guessing numbers
Age = 22
Counter = 0
For I in range (10):
If counter <=3:
guess_num = Int (input ("Input your guess num:"))
if Guess_num = = Age:
Print ("congratulations!")
Break #不向后走, exit immediately;
Elif Guess_num > Age:
Print ("num is larger!")
Else
Print ("num is smaller!")
Else
Continue_confirm = input ("Do want to continue?y/n")
if continue_confirm = = ' Y ':
Counter = 0 #重新为counter赋值, and then cycle from 0 onwards;
Continue #跳出当次循环, do not go next counter;
Else
Print ("Bye")
Break #退出循环;
Counter +=1 #循环一次counter自动加1;
This is Python some simple introductory learning, I believe that as long as there is a programming basis should know how simple Python is!
Here's an exercise:
Write the login interface, when the user entered 3 errors after the lock account, not login;
Write level three menu, can return to the upper level, can exit directly;
Learn to get started with Python