Inking Python notes (1): Basics

Source: Internet
Author: User

1. Type () to see the types of variables.

>>> Type (12.1)<class 'float'>>>> Type ('Hello')<class 'Str'>>>> Type (2)<class 'int'>>>> Type ('2')<class 'Str'>

2. Python 's variable names support numbers, letters, and underscores, but cannot start with a number.

3. expressions and declarations.

>>> n = 17>>> n17>>> 42+nprint(n)17

< Span style= "Font-size:small;" > + " *" +" can be used as a string connector, " *" multiple strings can be copied.

>>> first='throat'>>> second='Warbler' >>> first+Second'throatwarbler'Spam' 'spamspamspam'

5. Three types of errors in the program:

syntax error ; B. run-time error ; C. semantic error

int float< The span lang= "ZH-CN" > function converts integer data, numeric strings, and so on to floating-point types. str function to convert its arguments to strings.

 >>> Int ( " 2   '   '  2>>> int (5.67)  5 >>> Float (32)  32.0>>> float ( '   

< Span style= "Font-size:small;" >

 >>> import   math  >> > a=100>>> b=10>>> ratio=a/b  >>> print  (10*math.log10 (ratio)) #  log10 ()  10.0>>> print  (Math.log (MATH.E)) #  log ()  1.0>>> math.exp (Math.log (10))  10.000000000000002>>> radians=0.7>>> print  ( Math.sin (radians)) #  sin (), or cos (), tan ()  0.644217687237691>>> math.sqrt (4)/21.0 

8. Definition of function, note that the function must be defined before the function is called

>>>defPrint_lyrics ():#Define a functionPrint("i ' am a lumberjack, and I ' M okay.")Print("I sleep all night and I work all day")... ... ...>>> type (print_lyrics)#View Type<class 'function'>>>> Print_lyrics ()#perform the defined functionI'am a lumberjack, and I'm okay. I Sleep all Night andI work all day>>>defRepeat_lyrics ():#defines a function that contains a function that has already been defined... print_lyrics () ... print_lyrics ( ) ...>>>repeat_lyrics () I'am a lumberjack, and I'm okay. I Sleep all Night andI work all Dayi'am a lumberjack, and I'm okay. I Sleep all Night andI work all day

9. functions with parameters

>>>defPrint_twice (Bruce):#define a function that prints two times... Print(Bruce) ...Print(Bruce) ...>>> Print_twice ('Spam'*) Spamspamspamspamspamspamspamspam>>>Print_twice (Math.Cos (Math.PI))-1.0-1.0>>> michael='Eric, the half a bee'>>>Print_twice (Michael) Eric, the half a beeeric, the half a bee>>>defCat_twice (PART1,PART2):#defines a function that calculates "sum" of two input parameters. .. cat= Part1 +part2 ... print_twice (cat) ...>>> Cat_twice ()33>>> Cat_twice ('Hello',' World') Helloworldhelloworld

The return value of the void function is None.

>>> result=print_twice ('Bing') bingbingprint(Result ) None

Inking Python notes (1): Basics

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.