Python basic syntax

Source: Internet
Author: User

1. Input and output cycle judgment

num=10Bingo=False whilebingo==False: #False的写法要注意 answer=input ()ifanswer<Num:Print 'too small'     ifAnswer>Num:Print 'too Big'     ifanswer==Num:Print 'BINGO'Bingo=true
 for  in range (1,21):                  #从1循环到20    print i                             #  Note that there must be a first line indent

Formatted output:

>>> num=18>>>Print 'My Age is%d'%Nummy Age is18>>>Print 'Today is%s.'%'Friday'Today isFriday.>>>Print "%s ' score is%f"%('Lily', 90.2) Lily'score is 90.200000>>>Print "%s ' score is%.2f"%('Lily', 90.2) Lily'score is 90.20
 for  in range (0,5):    for in range (0,i+1):        print'  * ',                 # to output on the same line, followed by a comma    print                       # output Empty, Will change line

2. The main record is different from the C language and where special attention is needed:

// divisible

* * exponentiation

Integers have no length limit, floating-point numbers are limited by length (16 digits after the decimal point)

Plural:

>>> 1j*1j ( -1+0J)

3. Import the module:

Import

①import Math #导入math中所有函数 Use MATH.SQRT () in the form of

②from Math Import * #导入math中的所有函数, can be used directly sqrt ()

③from Math import sqrt, tan #只导入sqrt和tan两个函数 recommended

>>> Import math>>> math.sqrt (5) 2.23606797749979>>> math.sqrt (2) *math.tan (22) 0.012518132023611912>>> from math import *>>> log (25+5) 3.4011973816621555>>> sqrt (4) *sqrt 20.0>>> from math import sqrt, tan>>> tan (*sqrt) (4) 4.474321888449484

4. String : "," "," "" "" "

Len (): The length of the string itself is returned by the shape of the string, not like C.

+: string concatenation

*: Multiple splicing

" " "What ' s your name?" I asked. " I ' m Han meimei. " " " ' \ n "What\ 's your name?" I asked.\n "I\ ' m Han Meimei." \ n"" Thesameline"This is thesame line '
str="What' s your name?" I asked. " I ' m Han meimei. " " " Print str

The results obtained:

"What ' s your name?" I asked.
"I ' m Han Meimei."

5. Help:

dir (): The module name after the module is imported in parentheses, listing all functions of the module

dir (__builtins__): See a list of Python built-in functions

Help (): The function name in parentheses to see the function's document string

Print (): printing function Document string

such as: Print (math.tanh.__doc__)

Print (bin.__doc__)

6. Type conversion:

float (): Converts integers and strings to floating-point numbers

str (): converting integers and floating-point numbers to strings

int (): Converting floating-point numbers and strings to integers discarding fractional parts of a string must look like an integer "123.5" is not possible int (' 325 ') is OK

round (): floating point to Integer quad six in 50% double unsupported string

>>> str (3.1415) ' 3.1415 ' >>> float (' 3 ') 3.0>>> int ("123") 123>>> int ("123.5") Traceback (most recent):  File "<pyshell#30>", line 1, in <module>    

7. Multiple assignments:

>>> x,y,z=1, "R",1.414>>> x1>>> y ' r ' >>> z1.414

Value of the interchange variable

>>> y,x=x,y>>> x ' r ' >>> y1

8.random

 from Import randinti=randint (5,10)print i

Python 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.