Basic Python Tutorial Learning Notes---(1) Basic syntax

Source: Internet
Author: User
Tags integer division square root cmath

Divide 1, two integers, the fractional part of the calculation result is truncated, the result is still an integer;

such as:1/2=0

2, integer and floating-point number division, or divide between floating-point numbers, the result of a fractional part, is still a floating point;

such as:1/2.0=0.5 1.0/2=0.5 1.0/2.0=0.5 1/2.=0.5

3. Double slash (//) to define the integer division operation

such as:1.0//2.0=0.0 1//2=0

4,% defines the remainder operation (modulo operation)

such as:10%3=1 2.75%0.5=0.25

5.* * define a power operation (exponentiation)

such as:2**3=8-3**2=-9

6, the input of the expression will be automatically converted into decimal, the input 8 is also automatically converted to decimal. Starting with 0x for the 0, starting with a 8 binary

7, variable definition: Variable name can include letters, numbers and underscores (_), variables cannot start with a number

X=3 is an assignment expression

8. In the interactive interpreter, the result of the input expression and the input statement is the same.

such as:print 2*2

And:2*2

The result will print 4

However, this is not the case in program writing, only the Write statement can finish printing output.

An expression is something, a statement is doing something.

9. Get user input

Use the input function to get user input

>>>x=input ("x:")

X:34

>>>y=input ("Y:")

Y:42

>>>print X*y

1428

x,y receives the user input value through two inputs, and then participates in subsequent expression operations

10. Several functions

Pow (A, b) =a**b-----exponentiation function

ABS (a)------absolute value function

Round (b)------Rounding the floating-point number to the nearest integer value

11, the integer division operation can intercept the fractional part, only the whole part of the

The round () function can round the rounding

the floor function of the math module allows for rounding down, such as:

>>>import Math

>>>math.floor (32.9)

32.0

Import the module using import first, and then follow the module . Function "format using the function of this module

The ceil function is relative to floor and can be rounded up to the smallest integer

If you do not want to use the module every time you call a function in a module . Function "format, you can use another form:

>>>from Math Import Floor

>>>floor (32.9)

32.0

The floor function can be used directly after the "From module import function" without requiring the module name prefix

use the sqrt () function to calculate the square root of a number, but only to handle positive numbers

For negative numbers, you can use the Cmath module (the module for complex operations) to achieve the root function

Such as:

>>>import Cmath

>>>CMATH.SQRT (-1)

1j

The imaginary numbers end with J or J , for example:

>>> (1+3j) * (9+4j)

( -3+31j)

When using this function, it is best not to use the "from module import function" form, because once used,sqrt can only be the sqrt function in the Cmath module , Instead of using the normal sqrt function in the Math module , the module is still used . Function "format. This allows the Cmath module and the sqrt () function in the Math module to be used simultaneously

the. Py is the script file suffix name

double-clicking a. py file in a Windows system opens a Python file from a cmd window, But after you enter a command or do not enter a command window, it is immediately closed, adding

Raw_input ("Press <enter>")

This sentence allows the window to not close

14, use single and double quotation marks can be used to represent the string, but note that the string itself contains the single and double quotation marks, this time either use double or single quotation marks, or use a backslash as an escape character, of course, you can also complete the output by string concatenation

15. String concatenation

>>> "Hello," + "world!"

' hello,world! '

>>>x= ' Hello, '

>>>y= ' world! '

>>>x+y

' hello,world! '

Printing a string using print

>>>print ' hello,world! '

hello,world!

As you can see, strings printed directly with Python are quoted, and the results printed with the print function are not quoted.

This is because all that is printed directly through the interactive command in Python preserves the state of that value in the Python code. Another example:

>>>10000l

10000L

>>>print 10000L

10000

This is similar to the two string function, the str () function , therepr () function

>>>print repr (' hello,world! ')

' hello,world! '

>>>print repr (10000L)

10000L

>>>print str (' hello,world! ')

hello,world!

>>>print Str (10000L)

10000

This is summarized as follows:str (),repr () are 2 ways to convert a Python value into a string , and the function Str () makes the string easier to read,repr () convert the resulting string to a valid python expression

two ways to receive input:input and raw_input

When input is received using input, the user must enter a valid python expression, which is a string enclosed in single quotation marks (double quotes)

Otherwise you will get an error.

When using raw_input to receive user input, you can use no single or double quotation marks to indicate the string. raw_input automatically adds quotation marks to a string when it is received.

Therefore, you should use the Raw_input function as much as possible unless you have a particular need for input.

18, long string

If you need to write a long, long string and need to span multiple lines, you can use three single quotes or three double quotation marks instead of normal quotes

Like what:

With three single quotation marks (double quotes), there is no need to worry about the internal string itself if it contains single or double quotation marks, and it does not have to be escaped with escape characters.

19. Normal strings can also be wrapped by a backslash, which requires writing a backslash at the end of a line.

This time, the backslash only prompts for the meaning of line break, and will not be printed out.

20. Original string:

When a backslash is included in a string, it is considered an escape character. Such as:

\ n as a newline character, you can complete a line break in any string where it needs to be wrapped, \ ' is escaping ', meaning ' is part of the string itself, not the terminator of the string

But sometimes we need to enter the backslash itself, such as some directory path

The output we want should be ' c:\nDrivers '

But Python considers the backslash to be an escape function.

There are two solutions, one is to use a backslash to escape the backslash itself. Such as:

But when the string inside itself with a lot of backslashes, it is very troublesome. The second workaround is to use the original string notation.

The original string starts with R and can put any character in the string, and the only requirement is that the last character is not allowed to be a backslash. If the only requirement is met, you can assume that any character in a string is part of the string itself.

21. Unicode string:

is not the same data type as a string, starting with u, for example: U ' hello,world! 'ordinary strings in Python are stored in ASCII code 8, but Unicode strings are stored in 16 Unicode characters. all strings above python3.0 are Unicode strings. 22, Summary:algorithm--is a way to describe how to accomplish a taskexpressions-constructed with operators and functions, which can also contain variables that represent valuesvariable--used to represent a valueStatement--instructions to tell the computer to do somethingfunction--can have parameters, can return a valuemodule--Extend Python's functionality by importing it into Pythonstring--textShortcomings, please also advise. Reprint please specify the source!

Python Basic Tutorial Learning Notes---(1) Basic syntax

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.