day01 Python Data Type Notes

day01 Python Data Type Notes

Today Executive Summary

  1. variable
  2. constant
  3. Note
  4. Basic data types
  5. User input
  6. Flow control statements

Yesterday Recap

  1. The basic operation using the cloud code
  2. Markdown language and use Typora
  3. Recommended platform and production techniques of mind mapping
  4. The production of standardized notes
  5. Blog platform to introduce and production methods
  6. BUG row processes and techniques

Today's detailed content

variable

Introduce a printfunction to print (output) data.

>>> print(111)
111

The official definition of variables: the program running in the middle value, temporarily stored for reuse.

Popular terms, is to give the variable data from a nickname, easy call.

The following is a typical variable assignment statement:

name = "alex"

Where alexis the value assigned to a variable, which is the data; =represent assignment; nameis a variable name. By print(name)use variable names defined actions:

>>> name = "alex"
>>> print(name)
alex

Variable names naming convention:

  1. Variable names can only numbers, letters and underscores
  2. You can not begin with a number
  3. Prohibit the use of python keywords
  4. Variable names may have to be descriptive
  5. Variable names are case-sensitive
  6. You can not use Chinese and Pinyin
  7. Recommended wording:
    • Hump ​​body
    • Underline (the official recommended)

To start with a number of named variables will complain:

>>> 1a = 'alex'
  File "<stdin>", line 1
    1a = 'alex'
     ^
SyntaxError: invalid syntax

And underline hump body named variables example, it is clear that the nomenclature underlined more intuitive manner:

AlexOfOldboy = 89   # 这是驼峰体
alex_of_oldboy = 89 # 这是下划线

Python variable names are case sensitive:

>>> name = 'alex'
>>> Name = 'leo'
>>> print(name)
alex
>>> print(Name)
leo

Python keywords:

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or','pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

printYou can print multiple contents, separated by commas:

>>> age1 = 19
>>> print(age, age1)
18 19

Consider the following period of assignment:

age = 18
age1 = age
age2 = age1
age = 20
age1 = 19
print(age,age1,age2)

The final printed result is:

20 19 18

The specific assignment shown below:

QQ picture 20190905213207

The first step, to open up a memory in memory, to 18store in memory. Then the variable names agepoint to 18the corresponding memory address. Second and third step, age1and age2also point to 18the memory address. The fourth step, variable names agepointing to 20the corresponding memory address, but no longer points 18. The fifth step, variable names age1pointing to 19the corresponding memory address, but no longer points 18. The end result is, age2point 18, age1point 19, agepoint 20.

constant

In Python, there is no constant strict sense. Everyone convention is variable variable name in uppercase is considered constant, can not be easily modified during development. E.g:

ID = 110120130140150
ID = "123123213" # (不建议修改)

Variable and constant application scenarios:

  • We variable for use later in the development
  • Constants for the configuration file

Note

The role of the comment is to give some of the obscure code annotation or explanation. Code comments will not be executed.

Comments in Python divided into two types:

  • Single-line comments (comment when the line): The #beginning of the representation
  • Multi-line comments: The three pairs “ ”or ‘ ’package, you can wrap

Specific examples are:

# 这个是单行注释的示例
# 换行之后要在开头加一个#

"""
窗前明月光,
玻璃好上霜.
要不及时擦,
整不好就脏.
"""

Basic data types

In the Python-based data partitioning (data type) a total of seven species, three of which are focused on today: str (string), int (integer), bool (Boolean value)

Integer

Integer data in Python keywords int. The main purpose of the integer data are calculated and compared.

The basic usage data and integer operations are as follows:

a = 10
b = 5
print(a - b)
print(a + b)
print(a * b)  # * 乘
print(a / b)  # / 除

String

String in Python keywords str. In Python, as long as the string is in quotation marks.

Use Example string:

a = "你好"
b = '你好'
"""你好"""    # 三引号可以表示多行字符串,多行注释的原理就是一个未赋值的字符串
'''你好'''
print(a,b)
a = "123"
b = 123

It should be noted that the use of printa function, without the quotation marks at both ends of the variables:

# 你们会出现的问题
a = "alex"
b = "123"
print("a,b")
print(a,b)

String of +operations:

a = "alex"
b = "三哥"
c = a + b  # 字符串拼接
print(c)

a = "alex  dsb"
b = "三哥"
c = a +  b  # 字符串拼接
print(c)

String *operation

a = "坚强"
print(a * 8)   # 字符串的乘法

a = "坚持"
b = "Python26"
print(a + b * 5)

Summary string operations:

  • +Stitching: to be summed are strings
  • *Splicing: strings and numbers are multiplied

Boolean value

Boolean values true and false to indicate programming. In Python, with Truerepresentation; by FalseFIG. Only in Python Trueand Falsethe first letter is capitalized. Examples are as follows:

print(3 > 2)  # True  成立
print(2 > 3)  # False 不成立

User interaction

In Python, with the input()realization of interaction between the user and the program functions. inputInput means. Use example:

qq_user = input("QQ账号:")  # 坑 -- 阻塞
qq_pwd = input("QQ密码:")
print(qq_user,qq_pwd)

When the program to run inputwhen the statement, obstruction occurs, wait for user input. Program will remain blocked unless the user input content or terminate the program.

It should be noted that, in Python 3 inputcontent acquisition of all strings. Because of this reason, the following program will complain:

num = input("请输入数字:")
print(num + 5)

It is intended two lines of code, when the user inputs a number, the program automatically outputs a digital input digital large than 5. However, since the inputcontent acquisition is a string, the string is not an integer and add numbers and operation, and therefore the program being given. We can use the tpye()data type of the function to view variables.

num = input("请输入数字:")
print(type(num)) # 查看数据类型

输出的结果为:
请输入数字:12
<class 'str'>

By using int()a string of data can be converted to an integer function. Likewise, use str()the function to convert the string into integer data.

a = int('12')   # 字符串转成整型
b = str(23)     # 整型转成字符串

It should be noted that the use of int()the function to convert a string when integer, string contents must all are numbers, otherwise it will error:

>>> int('abc123')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc123'

So our example above can be changed like this:

num = input("请输入数字:")
a = int(num)
print(a + 5)

In this case, when we enter a number, by int()function, typing converted into an integer, then you can add and operation of:

请输入数字:3
8

You can also inpu()operate directly into int()the function:

num = int(input("请输入数字:"))
print(num + 5)

User interaction Summary:

  • input()Is the input, access to the contents are strings
  • type()Function is used to view the data type
  • int('字符串')Can be converted to an integer string, the string must all contents are digital
  • str(整型)Integer function can be converted into a string

Flow control statements

Flow control statements, conditional statements i.e., by selecting the determination, the content of the next decision operation. For example: If you are a man, you see me.

Keywords are flow control statements if, Shi 如果meaning. Flow control statements use the colon :indicates the end of the statement.

Python use indentation reflects the relationship between the dependent code. Generally use four spaces or a Tabbond represents one indentation. Note that, when programming, do not mix Tabkeys and the space, or if an error is difficult to find the problem.

Knowledge points to add: in Python, =represents the 赋值value of the variable to the right of the operation, will be assigned to the left side of the equal sign equal sign. And ==it represents the value determination on both sides are equal, i.e. 等于meaning.

Single if

Single ifflow control statements pseudocode format:

如果 条件:
缩进 结果

Specific examples are as follows:

sex = "男"
if sex == "男":
    print("就来看我")
print(sex)

输出的结果为:
就来看我
男

if else a second election

if elseChoose one flow control statements pseudocode format:

如果 条件:
缩进 结果
否则:
缩进 结果

Specific examples are as follows:

print(111)
if 3>2:
    print(11)
    print(22)
else:
    print(333)
print(444)

打印出的结果为:
111
11
22
444

if elif elif a multiple choice or zero

For if elif elifmultiple-choice or a zero in terms of flow control statements, as long as a condition is met, other statements will not be executed. Which pseudocode format:

如果 条件:
缩进 结果
再如果 条件:
缩进 结果
再如果 条件:
缩进 结果
再如果 条件:
缩进 结果

Specific examples are as follows:

if 3>5:
    print(1)
elif 3>7:
    print(2)
elif 5>2:
    print(4)
elif 3>1:
    print(5)
    
输出结果为:
4

if elif elif else a multiple-choice

if elif elif elseA multiple-choice format pseudocode for the flow of control statements:

如果 条件:
缩进 结果
再如果 条件:
缩进 结果
再如果 条件:
缩进 结果
否则:
缩进 结果

Specific examples are as follows:

if 3>12:
   print(1)
elif 3>11:
    print(2)
elif 4>12:
    print(3)
else:
    print(5)

输出的结果为:
5

if nested:

ifNested process control statements pseudocode format:

如果 条件:
缩进 如果 条件:
     缩进 结果

Specific examples are as follows:

sex = "男"
age = 48
if sex == "女":
    if age == 18:
        print("进来坐坐")
    else:
        print("隔壁找三哥")
else:
    print("去对门找alex")
    
输出结果为:
去对门找alex

if if if more than multiple-choice

if if ifMulti-selected multi-process control statements pseudocode format:

如果 条件:
缩进 结果
如果 条件:
缩进 结果
如果 条件:
缩进 结果

Specific examples are as follows:

if 43>1:
    print(11)
if 43>2:
    print(11)
if 43>3:
    print(11)
    
输出的结果为:
11
11
11

Postscript: Character andis meant. Only when the andvalue of both ends for the time, will return True, otherwise it will return False:

user = input("username:")
pwd = input("password:")
and 是和
if user == "alex" and pwd == "alex123":
    print(111)

Guess you like

Origin www.cnblogs.com/shuoliuchina/p/11479901.html