3. Introduction to Python __python

Source: Internet
Author: User
Tags floor division
3. Python Introduction

In the following example, the input and output pass the presence of a prompt (>>> and ...). To differentiate: If you want to repeat the example, you must enter everything after the prompt, and the line that does not begin with a prompt is the interpreter's output. Note that from the prompt in the example means you must add a blank line at the end; This is used to end a multiline command.

Many of the examples in this manual, even the examples entered interactively, are annotated. The annotation in Python starts with the number #, until the end of the physical line. Comments can start at the beginning of a line, or after a blank or code, but cannot be included in a string. Because annotations are meant to interpret code and are not interpreted by the Python interpreter, you can ignore them when you are typing the example.

For example:

# This is the ' comment
spam = 1 # and this is the  second comment # ... and now
          a third!
Text = "# This is not a comment because it ' s inside quotes."
3.1. Python as a calculator

Let's try some simple Python commands. Start the interpreter and wait for the main prompt >>>. (It doesn't take long.) ) 3.1.1. Digital

An interpreter can be used as a simple calculator: You can enter an expression into it and it will return its result. The expression syntax is very simple: the operator +,-, * and/is used in the same way as other languages (for example, Pascal or C); Parentheses (()) can be used for grouping. For example: >>>

>>> 2 + 2
4
>>> 50-5*6
>>> (50-5*6)/4
5.0
>>> 8/5
  # division always returns a floating point number
1.6

integers (such as 2, 4, 20) are of type int, and numbers with fractional digits (such as 5.0, 1.6) belong to float floating-point types. Later in this tutorial, we'll see more about the number type.

Division (/) always returns the number of float types. To do floor division and get an integer result (the integral part of the return quotient), you can use the//operator;% to calculate the remainder: >>>

>>> 17/3  # Classic Division returns a float
5.666666666666667
>>>
>>> 3  # Floor division discards the fractional part
5
>>>% 3  # The% operator returns T He remainder of the "Division
2
>>> 5 * 3 + 2  # result * divisor + remainder
17

Calculate n by * * [1]: >>>

>>> 5 * * 2  # 5
squared
>>> 2 * 7  # 2 to the power of 7
128

The equal sign (=) is used to assign a value to a variable. After you assign a value, no results are displayed until the next prompt: >>>

>>> width =
>>> height = 5 * 9
>>> Width * Height
900

If the variable is not "defined" (that is, not assigned), the error will be used:>>>

>>> N  # Try to access a undefined variable
traceback (most recent call last):
  File "<stdin>", Line 1, in <module>
nameerror:name ' n ' are not defined

Floating-point numbers are fully supported, and the operator of the mixed type operand converts the integer operand to a floating-point number:>>>

>>> 3 * 3.75/1.5
7.5
>>> 7.0/2
3.5

In interactive mode, the last printed expression is assigned to the variable _. This means that when Python is used as a desktop calculator, it is easy to perform continuous computations, such as: >>>

>>> tax = 12.5/100
>>> price = 100.50
>>> price * Tax
12.5625
>>&G T Price + _
113.0625
>>> Round (_, 2)
113.06

The user should treat this variable as read-only. Don't try to assign a value to it-you will create a separate local variable with the same name and mask the magic effect of the built-in variables.

In addition to integer and floating-point, Python supports other types of numbers, such as decimals and fractions. Python also has a complex number with built-in support and uses a J or J suffix (for example, the imaginary part of the indicated 3+5j). 3.1.2. String

In addition to numeric values, Python can also manipulate strings, which can be represented in several ways. They can be enclosed in single quotes (' ... ') or double quotes ("..."), which are equivalent to [2]. \ can be used to escape quotation marks:>>>

>>> ' spam eggs '  # single quotes
' spam eggs '
>>> ' doesn\ ' t '  Quote ...
" doesn ' t '
>>> "doesn ' t"  # ... or use double quotes instead
"doesn ' t"
>>> ' "Yes," he said. '
" Yes, "he said.
" >>> "yes,\" he said ""
" Yes, "he said.
" >>> ' "isn\ ' t," she said. "
Isn\ ' t, "she said."

In an interactive interpreter, the output string is enclosed in quotation marks, and special characters are escaped with backslashes. Although it may not look the same as the input (the enclosed quotation marks may change), two strings are equal. If the string has only single quotes but no double quotes, it is quoted in double quotes, or in single quotes. The print () function generates more readable output, by omitting quotes and by printing character escape and special characters:>>>

>>> ' "isn\ ' t," she said. "
Isn\ ' t, "she said.
" >>> print (' isn\ ' t, "she said. ')
" Isn ' t, ' she said.
>>> s = ' The Line.\nsecond line. '  # \ r \ n means newline
>>> s  # without print (), \ n is included in the output
' the ' the ' I line.\nsecond line. '
>>> Print (s)  # with print () \ n produces a new line a
.
Second line.

If you do not want a character that will be interpreted as the beginning of a special character, you can use the original string by adding R:>>>

>>> print (' C:\some\name ')  # here \ means newline!
C:\some
ame
>>> print (R ' C:\some\name ')  # Note the R before the quote
C:\some\name
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.