day02_python quick start

1. Coding

When we enter a paragraph on the computer and save it to the computer, the Chinese memory will be converted into 01010100100101...this form in the computer, and finally stored on the hard disk.

There is such a concept of encoding (codebook) in the computer.

王		-> 	01010100101 010
敏		-> 	010010110 10101
...

There are many kinds of codes in the computer

每种编码都有自己的一套密码本,都维护着自己的一套规则,如:
	utf-8编码:
				王		-> 101010001(乱写的) 
				桂		-> 110101010(乱写的)
    gbk编码:
				王		-> 010101010(乱写的) 
				桂		-> 010100010(乱写的)
	所以,使用不同的编码保存文件时,硬盘的文件中存储的0/1也是不同的		

注意事项: Save the file in the form of a certain code, and open the file with this code in the future, otherwise there will be garbled characters.

2. First programming experience

  • 编码Must 保持一致: save and open the same, otherwise there will be garbled characters
  • The default Pyhton interpreter opens files in the form of UTF-8. If you want to modify Python's default interpreter encoding, you can do the following:
# -*- coding:utf-8 -*-

print("我是你二大爷")
  • After writing the code in Pycharm, if it is not standardized, you can click code-> above Reformat Code, and the software will automatically standardize the code for you

3. Type conversion

int() str() bool()

Three sentences to understand type conversion

  • 空字符串,0 以外All other types, except , are True when converted to Boolean .
  • When converting a string to an integer, only 988strings in the ' ' format (all numbers) can be converted to an integer, and others will report an error
  • If you want to convert it into a certain type, just 类型的英文wrap it in this way.
sr(...)
int(...)
bool(...)

4. Variables

Variables are actually aliases and nicknames in our lives. Let the variable name point to a certain value. The format is: [ 变量名=值], and the corresponding value can be manipulated through the variable name in the future.

name = '王不易'
print(name)  # 王不易
flag = 1>18  # 意思是将,右边的'1>18'返回的结果赋值给flag,所以flag=False
address = '浙江省'+'宁波市'  # 意思是先将右边的字符串进行拼接,然后返回给address 所以address = '浙江省宁波市'
number = 1 == 2  # 先看右边再看左边。number = False

4.1 Specification of variable names

Three specifications (as long as there is one, an error will be reported)

  • Variable names can only 字母、数字、下划线consist of .
  • ca 数字n't start with
  • cannot be used Python内置的关键词as a variable name
    • If you don’t know whether it is a built-in keyword, it doesn’t matter. When you use a built-in keyword as a variable name, Pycharm will give you an error
      insert image description here

三个建议

  • Underscore connection name (lowercase)
#  很长的变量名,中间用_连接
farther_name = 'wangbuyi'
class_name = '计算机2101'
  • See the name and know the meaning
#  让别人看见变量名就知道这是什么意思
age = 19
color = 'red'
name = 'wby'
  • hump
#  顾名思义,就是变量名中首字母大写
#  在程序中,要么选择下划线连接命名,要么驼峰命名,建议不要一起使用,容易混乱
StudentName = 'wby'
StudentAge = 18

4.2 Variable memory pointing relationship

By learning the above knowledge of variables, we have a preliminary understanding of variables. Next, we will learn variables from a slightly more advanced perspective, namely: memory pointers (how they are stored in the computer's memory).

Scenario 1

name = "wupeiqi"

Create an area in the computer's memory to save the string "wupeiqi", and the name variable name points to this area.
insert image description here

Scenario two

name = "wupeiqi"
name = "alex"

Create an area in the computer's memory to save the string "wupeiqi", and the name variable name points to this area. Then a domain is created in the memory to save the string "alex", and the name variable name points to the area where "alex" is located, and no longer points to the area where "wupeiqi" is located (the data that no one points to will be marked as garbage, which is explained by the automatic recycling)

insert image description here

Scenario three

name = "wupeiqi"
new_name = name

Create an area in the computer's memory to save the string "wupeiqi", and the name variable name points to this area. The new_name variable name points to the name variable, and because it points to the variable name, it automatically points to the memory area represented by the name variable.

insert image description here

Scenario four

name = "wupeiqi"
new_name = name
name = "alex"

Create an area in the memory of the computer to store the string "wupeiqi", the name variable name points to this area (gray line), then new_name points to the memory area pointed to by name, and finally creates an area to store "alex", let The name variable points to the region where "alex" is located.
insert image description here

Scenario five

num = 18
age = str(num)

Create an area in the computer's memory to save the integer 18, and the name variable name points to this area. Create a string "18" in memory based on the integer type 18 through type conversion, and the age variable points to the memory area where this string is stored.
insert image description here

So far, the explanation about the memory of variables has been finished. Since everyone is a beginner, the memory management of variables only needs to understand the above knowledge points. More about memory management, garbage collection, resident mechanism and other issues are in will be explained later in the course.

5. Practice today

  1. Talk about the coding you know and why the garbled characters appear?

    答:当编码形式不一样的时候就会出现乱码的情况
    
       编码相当于是一个`密码本`,其中存储着文字和01010的对应关系。
       乱码的出现时因为文件的存储方式和打开方式不一致导致。另外,如何数据丢失也可能会造成乱码。
       假如:
       	武,对应存储的是:100100001000000111。如果文件中的内容丢失只剩下100100001000000,则读取时候就可能出现乱码。
    
  2. What is the default encoding of the Python interpreter? How to modify?

    #  Python解释器默认编码是UTF-8
    #  -*- coding:gbk -*-
    
       Python解释器默认编码:utf-8
       在文件的顶部通过设置: # -*- coding:编码 -*- 实现修改。
    
  3. Use print to print out the following:

    ⽂能提笔安天下,
    武能上⻢定乾坤.
    ⼼存谋略何⼈胜,
    古今英雄唯是君。
    
       text = """
       ⽂能提笔安天下,
       武能上⻢定乾坤.
       ⼼存谋略何⼈胜,
       古今英雄唯是君。
       """
       print(text)
    
  4. Naming conventions and recommendations for variable names?

    答: 命名规范: 1. 不能以数字为开头;2. 只能是数字,字母,下划线组成;3. 不能以Python默认的关键字为变量名
    		建议: 1. 使用下划线来分割很长的变量名 2. 必须让人一眼就知道这个变量是什么意思
    
       三条规范(必须遵循,否则定义变量会报错)
         - 变量名只能由 字母、数字、下划线 组成。
         - 变量名不能以数字开头。
         - 变量名不能是Python内置关键字
       二条建议(遵循可以显得更加专业,不遵循也可以正常运行不报错)
       	- 下划线命名法,多个单词表示的变量名用下划线连接(均小写)
       	- 见名知意,通过阅读变量名就能知道此变量的含义。
    
  5. Which of the following variable names is correct?

    name = '武沛齐'  
    _ = 'alex'
    _9 = "老男孩"
    9name = "宝浪"
    oldboy(edu = 666
    
    T T T F F
    
  6. Set an ideal number such as: 66, let the user enter the number, if it is greater than 66, the guessed result is larger; if it is smaller than 66, the guessed result is smaller; if it is equal to 66, the guessed result is correct.

    n = input('请输入数字:')
    if int(n) < 66:
        print('猜小了')
    elif int(n) > 66:
        print('猜大了')
    else:
        print('猜对了')
    
  7. Prompt the user to enter "Dad" and judge whether the user input is correct. If yes, the prompt is really smart, if not, it prompts whether you are an idiot.

    n = input('请输入 "爸爸" :')
    if n != '爸爸':
        print('你是傻逼么?')
    else:
        print('真聪明')
    
  8. Write a program, the grades have ABCDE5 grades, and the corresponding relationship with the scores is as follows.

    A    90-100
    B    80-89
    C    60-79
    D    40-59
    E    0-39
    

    After asking the user to enter a number from 0-100, you can correctly print his corresponding grade.

       score = input("请输入分数")
       data = int(score)
       
       if data >= 90 and data <= 100:
         print("A")
       elif data >= 80 and data <90:
         print("B")
       elif data >= 60 and data <80:
         print("C")
       elif data >= 40 and data <60:
         print("D")
       elif data >= 0 and data <40:
         print("E")
       else:
         print("输入错误")
    
    score = input("请输入分数")
    data = int(score)
    
    if 90 <= data <= 100:
        print("A")
    elif 80 <= data < 90:
        print("B")
    elif 60 <= data < 80:
        print("C")
    elif 40 <= data < 60:
        print("D")
    elif 0 <= data < 40:
        print("E")
    else:
        print("输入错误")
    
n = input('请输入0-100的数字: ')
if int(n) >= 90:
    print('A')
elif int(n) >= 80 and int(n) < 90:
    print('B')
elif int(n) >= 60 and int(n) < 80:
    print('C')
elif int(n) >= 40 and int(n) < 60:
    print('D')
else:
    print('E')

Guess you like

Origin blog.csdn.net/m0_48936146/article/details/127820200