<<python Basic Tutorials >> Learning Notes | The No. 01 Chapter | Basic knowledge

Source: Internet
Author: User
Tags pow square root python script cmath

This study note is mainly used to record the basic course of learning <<python >> Some key point, or not how to understand the content, may be a bit messy, but more practical, easy to find.


The No. 01 Chapter: Basic Knowledge

------

Jython:python Java implementation, run in the JVM, relatively stable, but behind Python, the current version 2.5, TA (python+robot) will use

Ironpython:python's C # implementation, run at common Language runtime, faster than Python

>>> from __future__ Import Division
>>> 1/2
0.5

If the direct operation, the value is 0. Unless you enter 1/2.0, Python is counted as a no-point operation.

------

Variable name: Includes letters, numbers, underscores, and cannot start with a number. So here's the right statement

Correct: file_1; Dict2; _del

Error: 9jin; Hello world; G-z

------

The difference between input and raw_input:

1. If the input is a number, after input, the data type is int line, Raw_input, only the character type, must be converted with int (STR1), which should be noted in program programming.

>>> i = input (' Enter here: ')
Enter here:32
>>> type (i)
<type ' int ' >
>>> j = raw_input (' Enter here: ')
Enter here:43
>>> type (j)
<type ' str ' >


>>> raw_input (' Enter a number: ')
Enter a Number:4
' 4 '
>>> input (' Enter a number: ')
Enter a Number:4
4

2. Input cannot include special characters, translate characters, otherwise it will error, raw_input can.

>>> a = input (' Enter here: ')
Enter here:\n\t

Also note that if it is a string input, you must add a single quotation mark, or double quotation marks
>>> a = input ("Enter string:")
Enter string: ' Hello '

Traceback (most recent):
File "<pyshell#75>", line 1, in <module>
A = input (' Enter here: ')
File "<string>", line 1
\n\t
^
syntaxerror:unexpected character after line continuation character
>>> B = raw_input (' Enter here: ')
Enter here:*\n\c\t
>>>

3. Under Win double-click to run the program, if you want to see the interactive interface, to avoid the program flash past, and at the bottom of the Raw_input (' Press Enter to Exit<> ')

------

>>> Round (#遵循四舍五入)
1.0

>>> Math.floor (1.1), Math.floor ( -1.1) #floor始终是取小的, from the name can also know that the floor, of course, is the lowest

(1.0,-2.0)

------

Ask for negative square root error

>>> Import Math
>>> math.sqrt (-1)
Traceback (most recent):
File "<pyshell#94>", line 1, in <module>
MATH.SQRT (-1)
Valueerror:math Domain Error

Must be used Cmath

>>> Import Cmath
>>> cmath.sqrt
<built-in function sqrt>
>>> cmath.sqrt (-1)
1j

>>> (1+2J) * (3-4j) #复数的运算
(11+2J)

------

Shebang

If you want a Python script to run like a normal script, you can add #! at the beginning of the line and then use chmod to add executable permissions

For example, the following:

#!/usr/bin/env python

Or

#!/usr/bin/python

$ chmod +x./hello.py

$ hello.py #如果不加的话, must./hello.py or Python hello.py

------

Single quotation marks if the number of single quotation marks, must be translated

"'" "three quotes, you can output strings in accordance with their own, the WYSIWYG pattern can also contain single quotes, double quotation marks

' Let\ ' Go '

' Let ' s go '

>>> print "" "This is a first programing ...
Print "hello,world!"
Print "Let ' s Go"

"""

Output Result:

This is a first programing ...
Print "hello,world!"
Print "Let ' s Go"


>>> Print str ("hello,world!")
hello,world!
>>> print repr ("hello,world!")
' hello,world! '

STR (), repr and anti-quotes are the three methods of converting values to character variables

Use ' anti-quote ' to calculate the arithmetic value, or reference a variable with a numeric type

>>> print ' 2+3 '
5

>>> temp = 2
>>> print "Temp ' s value is:", temp
Temp ' s value is:2
>>> print "Temp ' s value is:" + str (temp)
Temp ' s value is:2
>>> print "Temp ' s value is:" + ' temp '
Temp ' s value is:2

>>> print "Temp ' s value is:" + repr (temp)
Temp ' s value is:2

------

\ used for escaping in rows, such as \ n for newline, \ t tab, etc., at the end of line, can be ignored, mainly used for multi-line input. Such as:

>>> 1 + 2 + 3 \
-4/2 \
* 2% 3
5.0

>>> print "C:\\python27"
C:\python27

This means that you cannot enter \ At the end of the string

>>> print "This is illegal.\"
Syntaxerror:eol while scanning string literal

If you really want to say it, you need this:

Print r' C:\python27\lib\test\pack1\pack2\pack3 ' \ \ '
C:\python27\lib\test\pack1\pack2\pack3\

If it is a long path, so input is more troublesome, if the trouble is in front plus r, raw, raw string.

Indicates that the original string is useful, especially in regular expression matches, to remain intact.

>>> print ' C:\\python27\\lib\\test\\pack1\\pack2\\pack3 '
C:\python27\lib\test\pack1\pack2\pack3
>>> print R ' C:\python27\lib\test\pack1\pack2\pack3 '
C:\python27\lib\test\pack1\pack2\pack3


Typically Python uses 8-bit ASCII to store data, and if you want to store it with Unicode characters, you can store 16 bits and store more characters. User similar to R

>>> u ' hello,world! '
U ' hello,world! '

The main built-in functions in this chapter

Function Name Action Example

========================================================================

ABS (number) returns the absolute value of the numbers >>> abs ( -2) #2

CMATH.SQRT (number) returns the square root of the numbers >>> cmath.sqrt ( -4) #2j

Float (object) converts strings and numbers to no-point >>> float (' 123 ') #123.0

Help () Interactive interface >>>help (Math.cmath) #或help ()

Input (prompt) Gets the user input >>>input ("Enter Nuber:") #见上文

Raw_input (Prompt) Gets the user input, the character type >>>raw_input ("Enter:") #见上文

Long (object) converts a string and a number to a long integer, >>> long (' 12345 ') #12345L

Math.ceil (number) returns the upper part of the count, no dot >>> math.ceil (1.4) #2.0

Math.floor (number) returns the lower part of the count, no point type >>> math.floor (-1.6) #-2.0

Pow (X,y[,z]) returns the Y power of X, then the z-modulo >>> pow (3,2,4) #1

String of Repr (object) return value >>> repr (1234) # ' 1234 '

Str (object) Converts the value to String >>> str (12345) # ' 12345 '

Round (Number[,ndigit]) rounding operations based on numbers of specified precision >>>round (3.14) #3.0

<<python Basic Tutorials >> Learning Notes | The No. 01 Chapter | Basic knowledge

Related Article

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.