A string notation 1, a single string
by print()
Adding a string to the parentheses, you can output the specified text to the screen. For example ‘hello, world‘
, the output is implemented in code as follows:
>>> print(‘hello, world‘)
2, multiple strings
print()
A function can also accept multiple strings, separated by a comma "," and can be connected to a string of outputs:
>>> print(‘The quick brown fox‘, ‘jumps over‘, ‘the lazy dog‘)The quick brown fox jumps over the lazy dog
print()
Each string will be printed sequentially, and a comma "," will output a space, so the output string is spelled like this:
Input:
Note that each line of code ends without punctuation
Output:
3. Escape character
A string is ‘
any text enclosed in single or double quotation marks "
, such as ‘abc‘
, and "xyz"
so on. Note that ‘‘
or ""
itself is just a representation, not part of a string, so the string is ‘abc‘
only a
, b
c
this 3 characters. If it ‘
is also a character, it can be ""
enclosed, such as the character that contains it,,, the space, the "I‘m OK"
I
‘
m
O
K
6 characters.
What if the inside of a string contains ‘
and contains both "
? Can be identified by an escape character \
, such as:
Input:
Output:
The escape character \
can escape a number of characters, such as a \n
newline, a \t
tab, and the character itself, \
so the character is the one that \\
\
can be used print()
in Python's interactive command line To print a string look at:
Input:
Output:
If there are a lot of characters in the string that need to be escaped, you need to add a lot \
, in order to simplify, Python also allows to use r‘‘
‘‘
A string that represents the inside of the default is not escaped, you can try it yourself:
If there is a lot of line wrapping inside the string, \n
writing in one line is not good to read, in order to simplify, Python allows ‘‘‘...‘‘‘
the format to represent multiple lines of content:
In the interactive command line, note that when you enter multiple lines of content, the prompt >>>
changes to ...
, prompting you to enter the previous line, note that the ...
prompt is not part of the code:
```
after entering the Terminator and parentheses )
, execute the statement and print the result.
If a program is written as a .py
file, it is:
Input:
Output:
Note: Multi-line strings ‘‘‘...‘‘‘
are used in front r
of each other, and line breaks are still in effect
Second, the number of the wording
p
rint()
You can also print an integer, or calculate the result:
>>> print(300)300>>> print(100 + 200)300
Therefore, we can 100 + 200
print the results of the calculation a little more beautiful:
>>> print(‘100 + 200 =‘, 100 + 200)100 + 200 = 300
Note that for 100 + 200
the Python interpreter to automatically calculate the result 300
, however, ‘100 + 200 =‘
it is a string rather than a mathematical formula, and Python treats it as a string, explaining the printing results yourself.
Three, Boolean value
A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True
False
two values, either, True
or, False
in Python, you can directly use True
and False
represent a Boolean value ( note case ). It can also be computed by Boolean operations:
Boolean values can be used and
, or
and not
operations.
Four, null value
A null value is a special value in Python, None
denoted by. None
cannot be understood as 0
, because 0
it is meaningful, and None
is a special null value.
In addition, Python provides a variety of data types such as lists, dictionaries, and also allows you to create custom data types, which we'll continue to talk about later.
V. variables
Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, a number, and _
cannot start with a number, such as:
In Python, an equal sign =
is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of a different type, which is called a dynamic language by the variable itself. For example:
Input: Output:
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
x = 10x = x + 2
If mathematically understood x = x + 2
that is not tenable in any case, in the program, the assignment statement evaluates the right expression first x + 2
, obtains the result 12
, and assigns the variable x
. Since x
the previous value is 10
, the value becomes after the value is re-assigned x
12
.
Finally, it is important to understand the representation of variables in computer memory. When we wrote: a = ‘ABC‘
The Python interpreter did two things:
A string is created in memory ‘ABC‘
;
A variable named in memory is created a
and pointed to ‘ABC‘
.
You can also assign a variable a
to another variable, which actually refers to the data that the variable points to b
b
a
, such as the following code:
Input: Output:
VI, Constants
So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names.
But in fact PI
it is still a variable, Python does not have any mechanism to ensure that it PI
will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, and if you must change PI
the value of the variable, no one can stop you.
Finally, explain why the division of integers is also accurate. In Python, there are two types of division, and one division is /
.
/
The division evaluates to a floating-point number, even if the two integers are evenly divisible, and the result is also a floating-point number.
There is also a division that is //
called a floor divide, where the division of two integers is still an integer.
The floor of an integer //
is always an integer, even if it is not endless. To do the exact division, you /
can use it.
Because //
division takes only the integer part of the result, Python also provides a remainder operation that can be used to divide the remainder of two integers, which is also an integer.
Vii. function 1, print () function
To print out the name
contents of a variable, you can also use a function, in addition to writing directly name
and then pressing ENTER print()
:
>>> print(name)Michael
With the input and output, we can change the last printed ‘hello, world‘
program to a somewhat meaningful program:
name = input()print(‘hello,‘, name)
Run the above program, the first line of code will let the user enter any character as their own name, and then into the variable, the name
second line of code according to the user's name to the user hello
, such as input Michael
:
C:\Workspace> python hello.pyMichaelhello, Michael
2. Input () function
What if you want users to enter some characters from the computer? Python provides one that allows the input()
user to enter a string and store it in a variable. For example, enter the user's name:
>>> name = input()Michael
When you type name = input()
and press ENTER, the python interactive command line is waiting for your input. At this point, you can enter any character and then press ENTER to complete the input.
Once the input is complete, there is no hint, and the Python interactive command line is back in >>>
state. So where is the content we just entered?
The answer is stored name
in the variable. You can enter the contents of a name
view variable directly:
>>> name‘Michael‘
However, when the program runs, there is no prompt to tell the user: "Hey, just enter your name", this looks very unfriendly. Fortunately, it allows input()
you to display a string to prompt the user, so we change the code to:
name = input(‘please enter your name: ‘)print(‘hello,‘, name)
Run this program again, you will find that the program runs, will first print out please enter your name:
, so that the user can follow the prompts, enter the name after the hello, xxx
output:
输入: 输出:
Each time you run the program, the output will be different depending on the user input.
At the command line, the input and output are so simple.
The simple function first says 2 kinds, and then the function is said separately.
3 Summary
Any computer program is to perform a specific task, with the input, the user can tell the computer program required information, with the output, the program can be run to tell the user the results of the task.
Inputs are input and outputs are output, so we refer to the input and output collectively as input/output, or abbreviated to IO.
input()
And print()
is the most basic input and output below the command line, however, the user can also complete the input and output through other more advanced GUI, for example, enter their name in a text box on the webpage, click "OK" to see the output information on the webpage.
Python---3 basic Input Method