Python Basics-01

Source: Internet
Author: User

Python variable name

The variables in Python are named, broadly similar to other languages, with the following naming conventions:

    • Variable names are any combination of numbers, letters, and underscores
    • The first character of a variable name cannot be a number
    • The System keyword cannot be set to the variable name

Tip:python system built-in keywords are as follows:

  

【‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘,

‘try‘, ‘while‘, ‘with‘, ‘yield‘】

 在设置变量的时候,一定要注意变量的命名尽量要有意义,而且站在兼容的角度来讲,应该让变量尽量避免中文,虽然在python3中中文变量可以解析。

Also, in Python, there are no keywords that can set constants, but it is customary to capitalize a variable's word if you need to define a value as a constant.

Python character encoding issues

In Python3, Unicode Chinese encoding is supported by default, but Chinese encoding is not supported in Python2, so it is not possible to use Chinese directly. If you are rashly in a variable or

If you use Chinese in other cases, you will be thrown an error. Therefore, the code should be encoded in the initial position of coding.

Such as:

  

#-*-Coding:utf-8-*-

而在python3中则不需要设置这样的代码。

Python comments

In Python, comments are divided into single-line comments and multiline comments.

A single-line comment is a separate #, as follows:

  

Multiline comments are three quotes in Python, either single or double quotation marks. Start and end with three quotation marks, as follows:

"        here is a multiline comment"

Multi-line output


In Python, three quotes can serve as a multi-line comment, in addition to the function of multi-line annotations.

msg = ' This is    test Hello,world '     print (msg)

The effect of the output is as follows:

Question of quotation marks in Python


In Python, there is no difference in parsing, as in PHP, but there is no significant difference between the two, but it is important to note that if a string uses single quotes, then in single quotes

You cannot use single quotes, and if you use double quotes, you cannot use double quotes inside double quotes.

User input

需求,根据用户输入来进行输出,可以使用input方法。

The code is as follows:
user = input ("username:") Password = input ("Password:") #打印输出用户输入的账号和密码print (user,password) 

formatted output

在上面的实例代码中,可以通过input 获取用户输入的内容,并且赋值给变量。那么在实际的应用中,还存在另外一种需求:将用户输入的诸多信息格式化输出。而应对这种需求我们就需要使用%S这样的一个占位符来进行设置。具体代码如下:


  

在上面的实例中,虽然%s 的功能类似于占位符,但是实际上%s应该是字符串的意思。那么常用的有如下:    %s  字符串    %d  数字    %f  浮点但是需要注意的是,如果在上面的实例中,把%s 换成%d ,那么输入得内容就必须是数字。但时在python中,用户所有的输入内容都默认为字符串类型,所以我们还需要在需要数字的地方将字符串转为数字,我们可以使用int()方法转换。Tip:需要注意的是,在python2.x 中,raw_input 等同于 python3.x 中的input方法。但时在python2.x中同样存input方法,但是却和python3.x中的input方法是不同的,在3.x中,在输入值得时候,可以直接输入值,那么输入的都会自动的变成字符串。而在2.x中,输入值得时候,必须在值得外面加上一对引号,否则会被解析成变量。


The second way to format the output

采用变量赋值的方式可以实现第二种方式的格式化输出
The code is as follows:

  

第二种方式与第一种方式相比较,实现的功能是相同的,但是在某些时刻,却必须要使用第二种方式。在第二种方式中,使用了format()方法,那么我们使用format()方法来实现格式化交互输出还可以变异成另外一种写法。

As follows:

  

 Tip:在python中,实现格式化输出还可以使用+,通过字符串拼接来实现,但是却不是很推荐这种方式,因为我们上面的两种方式在内存只是开辟一份内存空间,而使用+来实现拼接却需要开辟出多个空间。

 

Password cipher text display

In Python above, we found that when we entered the password, we showed clear text, and what we actually needed was ciphertext, so we needed to introduce Python's internal modules.
The code is as follows:

TIP: It is important to note that the modules introduced at this time are Python built-in modules that need to be imported in order to be used, and that the Getpass module cannot be parsed directly in the IDE's built-in debug environment Pycharm, requiring the use of CPython.

Process Control Statements

In Python, the Process Control statements do not use curly braces to set statements like other languages, but rather to handle code dependencies through forced indentation.

If ... else

For example, if in a Process Control statement: Else statement.
As follows:

It is important to note that in Python, if there is no indentation in place where indentation is required, the system does not react, but throws an error. This is a very important point to note. is also different from other languages.

Of course, except if. else, you can also use the IF: Elif. else..
As follows:

While loop

In the Process Control statement, in addition to the IF: Else, there is a while loop and other loops.

The While loop instance is as follows:

Of course, the above code can also be easily optimized:

According to the code above, if after the user tries three times, we output a hint after the execution of the code, we can write the following words:

In the code above, the other is mated to the other at the end of the while and else is not feasible in other languages, but Python supports this notation.

For loop

In Python's process control, as with other languages, there is a for loop.

As follows:

for I in range:    print ("Loop:", i)

The above example can also be changed to a for loop, as follows:

Libai_old_age = 25for i in range (3):    guess_age = int (input ("Guess Age:"))    if guess_age = = libai_old_a GE:        print ("Yes,you got It")        break    elif guess_age > libai_old_age:        print ("Think smaller (... ") Else : print (" Think bigger ... ") Else: print (' You have tried too many times...fuck off ')
            

At this point the code can also be used with for and else, but and while. else if the else branch of the for is not executed after the break statement is executed, it means that the effect of the code execution will not output the contents of the Else branch if the user inputs are correct.

In Python's for loop, there is another way to look at the example you just made:

for I in range:    print ("Loop:", i)

The output is 0-9, and if you want to output 0,2,4,6,8 and so on, in Python, it is much easier to implement than other languages.

For I in range (0,10,2):    print ("Loop:", i)

As above, it can be very simple to achieve a single output. You don't need to be judged like any other language.

Instance Upgrade:

After the user has guessed three age, let the user manually choose whether to continue, enter N, stop, enter continue.

As follows:

Break and Continue

In the above example, we used the break, when a break in the loop, it will jump out of the entire loop, the execution of the following code outside the loop, and in the process control, in addition to the loop there is another keyword, but also has a similar function, but not jump out of the entire loop, Instead of jumping out of the current loop process, it is continue.

As follows:

For I in range (0,20): if i = = 4:    print ("I am 4 ..." )    continueprint ("Loop:", i)  

Python Basics-01

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.