Python Learning Week1

Source: Internet
Author: User

What is Python?

is an object-oriented, interpreted computer programming language, invented by Dutchman Guido van Rossum in 1989, and the first public offering was released in 1991. (Excerpt from Baidu Encyclopedia)

Python the pros and cons

See the pros first

    1. Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
    2. Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
    3. High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
    4. Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
    5. Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
    6. Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.

Look at the disadvantages again:

    1. Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.01s, Python is 0.1s, so c directly than Python 10 times times faster, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. In fact, in most cases python has been fully able to meet your requirements for the speed of the program, unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
    2. Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
    3. Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil. A compromise solution to this problem is discussed in more detail later in the Threads and Processes section. (Excerpt from Alex Li blog)
Python Basics Hello World
1 # Print out Hello world! on the screen 2 Print ("Hello world! ")
Variable

A variable can only be a combination of letters, numbers, underscores

Variable cannot start with a number

System reserved keywords are not used as variable names

# define a variable, assign Hello world! to it, print it out a="Hello world! " Print (a)
User input

Use the input function to get the data entered by the user

# use the input function to get the Hello world! for user input Assign value to variable a a=input ("pleaseinput Hello world!:")print (a)
string concatenation
#string concatenation, the user input any character, directly generated Hello (str)! #Method OneA=input ("Please input your name:")Print("Hello"A"!")#Method TwoA= input ("Please input your name:") Info=" "Hello%s" "% (a) +"!"Print(Info)#Method ThreeA= input ("Please input your name:") Info=" "Hello {b}" ". Format (b=a) +"!"Print(info)

PS: the string is%s; integer%d; floating-point number%f.

Primary Knowledge Module
# user input user name and password, password requirements do not display, use Getpass module Import getpassusername=input ("pleaseinput username:") Password= Getpass.getpass ("Please input password:")print (" Username: ", username,"Password:", password)
If...else

Usage:

#If condition:#EXECUTE Statement#Else:#EXECUTE StatementA=int (Input ("Please input a number:"))ifA<3:   Print("Y")Else:   Print("N")#If condition:#EXECUTE Statement#elif:#EXECUTE Statement#Else:#EXECUTE StatementA=int (Input ("Please input a number:"))ifA<3:   Print("a<3")elifA>3:   Print("a>3")Else :   Print("a=3")

In combination with the above example:

#determine if the user entered the correct account and passwordImportGetpassusername="Kiwi"Password="kiwi123"_username=input ("plsease input Username:") _password=getpass.getpass ("Please input password:")ifUsername==_username andpassword==_password:Print("Welcome", _username)Else :   Print("worng Username or password,please input again!")
While

Usage:

#defines a variable count, with the condition that the command is executed in a loop that is always true#While True:#Execute CommandCount=0 whileCount=0:Print("A is apple;b is ball!")#define a variable count, if it is less than 3, print A is Apple, whereas print B is ball#While True:#Execute Command#Else:#Execute CommandCount=0 whileCount<3:    Print("A is Apple") Count+=1Else :   Print("b is ball") Count+=1#define a variable count, print 10 times A is Apple#While True#Execution Conditions#if equals 10 times# BreakCount=0 whilecount>=0:Print("A is Apple;") Count+=1ifcount==10:      Break

In combination with the above example:

#determine if the user entered the account and password is correct, if the input error 3 times, automatic exitImportGetpassusername="Kiwi"Password="kiwi123"Count=0 whileCount>3: _username=input ("plsease input Username:") _password=getpass.getpass ("Please input password:")   ifUsername==_username andpassword==_password:Print("Welcome", _username) Break   elifcount==2:         Print("So many worngs,please try again!")          Break   Else:         Print("worng Username or password,please input again!") Count+ = 1
For

Usage:

#Cycle 10 times forIinchRange (10):    Print("Loop:", i)#Loop 10 times, if I is less than 5, do not print forIinchRange (10):    ifI<5:        Continue    Print("Loop:", i)#Loop 10 times, if I is greater than 5, automatically exits forIinchRange (10):    ifI>5:         Break    Print("Loop:"I

In combination with the above example:

ImportGetpassusername="Kiwi"Password="kiwi123" forIinchRange (3): _username=input ("plsease input Username:") _password=getpass.getpass ("Please input password:")   ifUsername==_username andpassword==_password:Print("Welcome", _username) Break   elifi==2:         Print("So many worngs,please try again!")          Break   Else:         Print("worng Username or password,please input again!")

(The above code is written using Python3.6.1, pure hand knocking, ide:pycharm)

These are the Python learning notes of the week.

If there is a mistake, please correct me and learn from each other.

Life is short,you need python!

Life is short, I use python!

The job is as follows:

1. Writing the Login interface

    • Enter User name password
    • Show welcome message after successful authentication
    • Three-time error after locking
    • Need to use Python's own read file feature

2. Multi-level menu

    • Level three Menu
    • You can go to each submenu in turn and return to the top level menu

Python Learning Week1

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.