Basic Python Program Basics Tutorial

Source: Internet
Author: User

Python Program Basic composition

Python, like other high-level languages, is almost always the first to receive some data (such as a keyboard or a file or assignment) from somewhere, then make the necessary processing of the data, and then upload the results to somewhere (output to a file, database, or print to the screen).

The basic architecture of the Python program is as follows:

    1. Program Initialization Section

    2. Input part of program data

    3. Processing part of program data

    4. Output part of program data

    5. Program End Section

Data output

In the Python language, the output of the data can be done through the print function, and the syntax structure of the print function is as follows.


When Python executes the print statement, it first calculates the value of the expressions expression behind the print function and then prints out the value of the expression.


We notice the <expressio in the print syntax structure ns> the words behind the word s, plural, what do you mean? The meaning is that the expression can be multiple, multiple <express I o n> comma interval between.

>>>print ' Hello ' hello>>>print ' world ' world>>>print ' hello ' + ' world ' helloworld>> >print ' Hello ', ' World ' Hello World

Statements 1, 3 are printed separately ' Hello ' and ' world ' two strings, we can see each print statement output is automatically replaced by a line, the 5th line of the statement with the connection of two strings, the print output when the letter ' O ' and ' W ' is, and the 7th line statement is a comma concatenated with two strings, We found that the letters ' O ' and ' W ' were not next to each other but in the middle of the output, so we could understand the comma in the print function, and the comma in the print statement could not output the return key line, so that the next print data and this print statement output on the same line.

Here is a new data type string , which is quoted to enclose some characters, which we call the data as strings. There are three types of definitions of string data in Python:

    1. Enclose some characters in single quotation marks

    2. Some characters that are enclosed in double quotation marks

    3. Three double quotation marks are characters that are string data.

Why are there three double-quoted strings? This is so that you can use double quotes in string data.

>>>print ' Hello ' hello>>>print "World" World>>>print "" "and he said" Hello "to God" "he said" Hello "to God

Note that the double quotes around the hello string behind said are printed out at the output. How to implement string and digital data output at the same time?

We can use commas to concatenate strings and digital data, so that we can print both strings and digital data, and of course, two print statements.

>>> x = 12>>> print ' Hello x = ', Xhello x = 12

Referencing the format control Word%d in a string enables printing of shaping data,%f can print floating-point data, and%s is string data.

>>> x = 12>>> print ' Hello x = ', Xhello x = 12>>> y = 12.45>>> s = "World" >>&G T Print "Output%d%f%s"% (x, y, s) output 12.450000 world>>>

It is important to note that the 6th line of code, the value of the preceding double quotation mark is called the format control word of the print function, and the parentheses enclose the sequence of values corresponding to each% in the formatted control word.

Data entry

In Python, the user's data input can be obtained from the keyboard via the raw_input function, and the syntax structure is as follows.


The parameter prompt of the raw_input function is a string that prompts the user for input, and when the user enters the data, the raw_input function passes the input data to the variable to the left of the equals sign to save the input data, raw_input The return value of the function is of type string.

>>>name = raw_input ("plz input your name:") plz input your name:hello>>>print namehello

The statement code line 1th uses the raw_input function to receive user data, assigns a value from the raw_input function to the name variable, line 2nd enters the ' hello ' string, 3rd The line call print prints the value of the name variable. Let's look at the following program, the task is to enter two integers from the keyboard to x and Y, calculate the sum of x+y and print the results.

>>> x = raw_input ("Plz input x:") plz input x:12>>> y = raw_input ("Plz input y:") plz input Y:12&G T;>> z = x + y>>> print z ' 1213 '

Why is that so? Why not the result 25? Let's see another program.

>>> x = raw_input ("Plz input x:") plz input x:12>>>print x ' ">>> x = 12>>>print x1 2

Lines 4th and 7th are the results of printing after the ' Print X ' is executed, are there different two print results? The output of line 4th has a single quotation mark before and after 12 (the Raw_input return value is a string type), and the 7th line outputs 12 without a single quotation mark, so the return value of the raw_input function is a string, so how do we do our task? Here is a description of the type conversion function int function, theint function can convert a string composed of pure numeric characters into shaping data, so the task program can be changed to look like this.

>>> x = Int (raw_input ("Plz input x:")) plz input x:12>>> y = Int (raw_input ("Plz input y:")) plz inpu T y:13>>> z = x + y>>> Print Z25

Lines 1th and 3rd of the statement input string data from the keyboard through the raw_input function, the INT function converts a numeric string to the left variable x and Y after it is converted to the shaping data, and the X and Y variables store the shaping data 12 and 13.

In fact, there is another function in Python called input that can also capture data from the keyboard, whose return value is directly numeric, but the keyboard input must be a number (shaped or floating point).

>>> x = input ("Plz input x:") plz input x:12>>> y = input ("Plz input y:") plz input y:13.5>> > z = x + y>>> Print z25.5

Can I enter with a minus sign? The float function is described here to convert strings with a bit and full numbers into floating-point data.

>>> z = raw_input (' plz input a float: ') plz input a float: -12.45>>> print z ' -12.45 ' >>> w = Flo at (z) >>> print w-12.45

We're looking at how the input function behaves.

>>> x = input (' plz input an int: ') plz input an int:1234>>> print x1234>>> y = input (' plz inpu T a float: ') plz input a float: -9.8765>>> print y-9.8765


This article is from the "Python Training Zhipu Education" blog, so be sure to keep this source http://jeapedu.blog.51cto.com/7878264/1617004

Basic Python Program Basics Tutorial

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.