Python (2) package and basic data types

Insert package

import os
import requests
r=requests.get("https://cn.bing.com/")
print(r.url)
print(r.encoding)
print(r.text)

import requests

Unresolved import: requests
write picture description here
write picture description here
write picture description here
write picture description here

type of data

There are six standard data types in Python3:
Number (number)
String (string)
List (list)
Tuple (tuple)
Sets (collection)
Dictionary (dictionary)
Among the six standard data types in Python3:
immutable data (four A): Number (number), String (string), Tuple (tuple), Sets (collection);
variable data (two): List (list), Dictionary (dictionary).

print statement

The print statement can also follow multiple strings, separated by commas "," to form a string of output, print will print each string in turn, and a space will be output when a comma "," is encountered. Therefore, the output Strings are spelled like this


>>> print 300
300    #运行结果
>>> print 100 + 200
300    #运行结果

variable

  • variable name

In a Python program, a variable is represented by a variable name. The variable name must be a combination of uppercase and lowercase English, numbers and underscores "_", and cannot start with numbers

a = 1
The variable a is an integer.
t_007 = 'T007' The
variable t_007 is a string.

  • Assignment
    In Python, the equal sign = is an assignment statement, which can assign any data type to a variable. The same variable can be assigned repeatedly, and it can be of different types.
a = 123       # a是整数
print a
a = 'imooc'   # a变为字符串
print a

The language in which the type of the variable itself is not fixed is called a dynamic language, and the corresponding one is a static language. Static languages ​​must specify the variable type when defining a variable. If the type does not match when assigning, an error will be reported. For example, Java is a static language, and the assignment statement is as follows (// indicates a comment):

int a = 123; // a是整数类型变量
a = "mooc"; // 错误:不能把字符串赋给整型变量

This is why dynamic languages ​​are more flexible than static languages.

a = 'ABC'
b = a
a = 'XYZ'
print b

Output result: ABC
write picture description here
write picture description here
write picture description here

  • practise

The arithmetic sequence can be defined as the difference between each item and its previous item is equal to a constant, the variable x1 can be used to represent the first term of the arithmetic sequence, and d is used to represent the tolerance, please calculate the sequence 1 4 7 10 13 16 19 … the sum of the first 100 terms

d=1
c=100
sum=0
while c > 0:
    sum=sum+d
    d=d+3
    c=c-1
print(sum)

string

  • Escape Characters
    Earlier we explained what a string is. Strings can be represented by " or " ".
    What if the string itself contains '? For example, we want to represent the string I'm OK, at this time, it can be represented by " ":
"I'm OK"

Similarly, if the string contains ", we can use ' ' to express:

'Learn "Python" in imooc'

What if the string contains both ' and "?
At this time, it is necessary to "escape" some special characters of the string, and the Python string is escaped with \.
To represent the string Bob said "I'm OK".
Since ' and " will cause ambiguity, we insert a \ in front of it to indicate that this is a common character and does not represent the beginning of a string. Therefore, this string can be expressed as

'Bob said \"I\'m OK\".'

\n means a newline
\t means a tab character
\ means the \ character itself

  • Raw strings and multi-line strings
    If a string contains many characters that need to be escaped, it can be cumbersome to escape every character. In order to avoid this situation, we can add a prefix r in front of the string, indicating that this is a raw string, and the characters in it do not need to be escaped. For example: r'(~_~)/ (~_~)/'
str=r'\(~_~)/ \(~_~)/'
print(str)

Output: \(~_~)/ (~_~)/
But the r'...' notation cannot represent a multi-line string, nor can it represent a string containing ' and " (why?)
If you want to represent a multi-line string , which can be represented by "'..."':

str='''Line 1
Line 2
Line 3'''
print(str)

output:

Line 1
Line 2
Line 3
The string representation above is exactly the same as the following:

'Line 1\nLine 2\nLine 3'

You can also add r in front of a multi-line string to turn this multi-line string into a raw string:

r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''

Unicode string

Python later added support for Unicode. Strings expressed in Unicode are represented by u'...', such as: print u'Chinese'
Note: Chinese cannot be displayed normally without u.
Unicode strings are no different from ordinary strings except for one more u, escape characters and multi-line notation are still valid

u'''第一行
第二行'''

raw+multiline:

ur'''Python的Unicode字符串支持"中文",
"日文",
"韩文"等多种语言'''

If the Chinese string encounters UnicodeDecodeError in the Python environment, it is because there is a problem with the format of the .py file. Comments can be added to the first line

# -*- coding: utf-8 -*-
//u和上面这一句相同的意思,不要重复添加

boolean type

We have already seen that Python supports Boolean data. The Boolean type has only two values, True and False, but the Boolean type has the following operations:

AND operation: The result of the calculation is True only if both Boolean values ​​are True.

True and True     # ==> True
True and False    # ==> False
False and True    # ==> False
False and False   # ==> False

OR operation: As long as there is a Boolean value of True, the result of the calculation is True.

True or True     # ==> True
True or False    # ==> True
False or True    # ==> True
False or False   # ==> False

NOT operation: Change True to False, or False to True:

not True         # ==> False
not False        # ==> True
a = True
print a and 'a=T' or 'a=F'

The result of the calculation is not a boolean type, but a string 'a=T', why is this?
Because Python treats 0, the empty string" and None as False, and other numbers and non-empty strings as True

rue and 'a=T' The result of the calculation is 'a=T' Continue the calculation of 'a=T' or 'a=F' The result of the calculation is still 'a=T'
To explain the above result, it also involves an operation of and and or Important rule: short-circuit calculations.
1. When calculating a and b, if a is False, according to the AND operation, the whole result must be False, so return a; if a is True, the whole calculation result must depend on b, so return b.
2. When calculating a or b, if a is True, according to the OR algorithm, the entire calculation result must be True, so return a; if a is False, the entire calculation result must depend on b, so return b.

Therefore, when the Python interpreter performs Boolean operations, as long as the calculation result can be determined in advance, it will not calculate later and return the result directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325610032&siteId=291194637