Python experience (01)-variables, functions, and basic control statements

Source: Internet
Author: User

Debian5 was installed today. I didn't expect python to be installed in the basic environment, so I tried this legendary language.

The following is a series of simple examples to experience,CodeYou can directly paste and save the files, all of which pass the test.

For more information, see pdf. You can download a PDF file from this address.

Case 1:

# Used for comments, but the first line #! AlsoProgramIt is pointed out that this code is executed by/usr/bin/python, so the file name can not be py
If you are happy, you can add the property X to helloworld. py, so that you can change it to any file name for direct execution, such as hello.

# ! /Usr/bin/Python
# Filename: helloworld. py
Print   ' \ N \ thello world! That \ ' S python! \ N '
# If you want the. py file to be executable: chmod A + x helloworld. py

Case 2:

You can directly assign values to variables without declaration. If/Elif/else statements do not need to include judgment statements and end;

Code # ! /Usr/bin/Python
# Filename: raw_input.py

Number =   23   # Here no ;But you can add; either
Guess = INT ( Raw_input( " Enter an integer: " )); # String, can use 'Or"

If Guess = Number: # Notice: No ()And there's :
Print   ' Congratulations, you guessed it. '
Print   ' (But you do not win any prizes !) '
Elif Guess < Number:
Print   " No, it's a little higher than what you input. "
Else :
Print   ' No, it \ ' S a little lower than what you input. '

Print   " Done! " ;

 Case 3: Guess the number game and exit if the game is incorrect after five guesses

While loop, break jumps out; saves {} similar to the C language, and uses a pair to determine the statement Block

Code # ! /Usr/bin/Python
# Filename: While. py

Number =   23
Hitting =   False
Loop =   5

While   Not  Hitting:
Guess = INT (raw_input ( " Guess what ( "   + STR (loop) + " Times left ): " ))
Loop = Loop - 1 ;
If Loop = 0:
Print   " \ Ttoo stupid, you lost! "
Break ;
If Guess > Number:
Print   " \ Tyour number is bigger than that. "
Elif Guess < Number:
Print   " \ Tyour number is littler than that. "
Else :
Hitting =  True;
Print   " \ Tyou win, excellent baby! "
Print   " \ T -- = -- [done] -- = -- "
# The done statement is line to while, so running out of while then "done" execute.

Example 4: function, default parameter, return value; docstring

Code # ! /Usr/bin/Python
# Filename: function. py

Def Value (Num =  3 ): # There's Default Parameter.
''' Coded by Phoenix:

RET (x) = x ^ x '''   # Docstrings
RET =   1 ;
For I In Range (0, num):  # Notice, there is:
RET = RET * Num
Return  RET

Number = INT (raw_input ( " Input a number: " ))
Print Value ()
Print Number ** Number
Print Value (number) # Output the x ^ X, by the way, you can use: X ** x
Print Value. _ Doc __   # Output docstrings

 

 

 

 

 

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.