Review Python BASICS (3)

Source: Internet
Author: User

1. Python input raw_input () function.

 
A = raw_input ()#The result is a string.A = int (raw_input ())#If you want to get an int, you can only do this.

If special characters such as \ t \ n are input, % R outputs \ t, \ n, and % s outputs \ t and \ n.

View code

 Print   "  How old are you?  "  Age = Raw_input ()  Print   "  How tall are you? "  Height = Raw_input ()  Print   "  How much do you weight?  "  Weight = Raw_input ()  Print   "  So, you are % R old, % R tall and % R heavy.  " % (Age, height, weight)  Print  "  So, you are % s old, % s tall and % s heavy.  " % (Age, height, weight)  Print   "  So, you are % d old, % d tall and % d heavy.  " % (INT (AGE), INT (height), INT (weight ))

Output

View code

How old are you? \ Thow tall are you? \ Nhow much do you weight?111So, you are'\ T'Old,'\ N'TallAnd '111'Heavy. So, you are \ t old, \ n tallAnd111 heavy.

This is usually the case.

View code

Age = raw_input ("How old are you?") Height= Raw_input ("How tall are you?") Weight= Raw_input ("How much are you weight?")Print "So, you're % R old, % R tall and % R heavy."% (Age, height, weight)

Differences between input and raw_input

When the input is a pure number

    • Input returns numeric values, such as int and float.
    • Raw_inpout returns the string type and the string type.

Input string as expression

Input calculates the number expression in the string, but raw_input does not.

For example, enter "57 + 3 ":

      • Input returns an integer of 60.
      • Raw_input will get the string "57 + 3"

Actually

 
DefInput (prompt ):Return(Eval (raw_input (prompt )))

2. Run the following command to view the doc in Windows:

 
Python-M pydoc raw_input

3. How to Write parameters when running a Python script.

ViewCode

 From Sys Import  Argvscript, First, Second, Third = Argv Print   "  The script is called:  "  , Script  Print   "  Your first variable is:  "  , First  Print   "  Your second variable is:  "  , Second  Print   " Your third variable is:  " , Third

Argv is similar to main (string [] ARGs) in Java ). But argv needs to specify the number of parameters, and the number of parameters must match when running the script. Otherwise, an error is returned. And the first parameter is ex13.py in Python ex13.py 1 1.

If the number of parameters is incorrect, the output is as follows:

View code

 
E: \ SkyDrive \ Python \ the hard way to learn Python> Python ex13.py 1 2 3 4Traceback (most recent call last): File"Ex13.py", Line 2,In<Module>Script, First, Second, Third=Argvvalueerror: too has values to unpack

4. Read and print the file

When reading a file from a script, it is best to use the file name as the script parameter. The following is the code

FromSysImportArgvscript, filename=Argvtxt=Open (filename)PrintTXT. Read ()
TXT. Close () file_name_again= Raw_input ("The filename you want to print \ n>")PrintOpen (file_name_again). Read ()

Output

View code

E: \ SkyDrive \ Python \ the hard way to learn Python>Python ex15.py ex15_sample.txtthisIsStuff I typed into a file. ItIsReally cool stuff. LotsAndLots of fun to haveInHere. The filename you wantPrint>Ex15_sample.txtthisIsStuff I typed into a file. ItIsReally cool stuff. LotsAndLots of fun to haveInHere.

 

 

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.