Basic knowledge of notes in the basic python tutorial and basic python tutorial

Source: Internet
Author: User

Basic knowledge of notes in the basic python tutorial and basic python tutorial

  • Digit-related

Add from _ future _ import division to the Python program, directly execute it in the interpreter, or use the command switch-Qnew when running Python through the command line, the single slash will not be divisible, as shown in figure

>>> 1/2
0.5

The dual-slashes implement division, as shown in figure

>>> 1 // 2
0

  • Input

>>> X = input ('x :')

  • Module

Import the module with import, and then use the function of this module in the format of "module. function", as shown in

>>> Import math
>>> Math. floor (23.96)
23.0

After using the import command in the form of "form module import function", you can directly use the function without using the module name as the prefix. For example

>>> From math import sqrt
>>> Sqrt (9)
3.0

The CMath module can handle the complex numbers, for example

>>> Cmath. sqrt (-9)
3j

  • Program

In IDLE, file-> new file will pop up an editing window, which is edited as follows:

name = raw_input("what is your name")print 'hello,' + name + '!'

Then save file-save as hell. py, and run F5. The result is displayed in the interpreter, as shown in

>>>================================== RESTART ==== ======================================
>>>
What is your name55
Hello, 55!

Alternatively, double-click hell. py to flash the command line window.

 

In the code, # everything on the right is ignored, and that part is the comment.

  • String

Reverse quotation marks, str, and repr are three ways to convert a value to a string. str converts the value to a string in a reasonable form for your understanding, and repr creates a string, the value is expressed in the form of a valid flat Python expression, as shown in figure

tmp = 1000print 'hello ' +  `tmp`print 'hello ' +  str(tmp)print 'hello ' + repr(tmp)

The result is as follows:
Hello 1000
Hello 1000
Hello 1000

 

Input assumes that the user inputs a valid Python expression (more or less the opposite of the repr function). raw_input regards all input data as the original data and places it in the string, as shown in

print 'hello ' + raw_input('your name:')print 'hello ' + input('your name:')

The result is as follows:

Your name: 55
Hello 55
Your name: 55

Traceback (most recent call last ):
File "E:/work/Code/python/hell. py", line 2, in <module>
Print 'hello' + input ('your name :')
TypeError: cannot concatenate 'str' and 'int' objects

 

If you want to write a long string that spans multiple lines, you can use three quotation marks to replace common quotation marks, such

print '''This is a long string.it continues here.and it's not over yet."hello world!".still here.'''

The result is as follows:
This is a long string.
It continues here.
And it's not over yet.
"Hello world! ".

A normal string can also be a cross-line string. If the last character in a line is a backslash, the line feed itself will be "escaped", that is, ignored (also applies to expressions), as shown in

print 'This is a long string.\it stop here.'

Result

This is a long string. it stop here.

The original string does not use the backslash as a special character. Each character entered in the meta-string is consistent with the writing method, as shown in figure

print r'E:\work\Code\python' '\\'

The result is as follows:

E: \ work \ Code \ python \

 

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.