Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others
All Blogs My Verse

Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others

Day1-Python-Basics

2020-05-15

Python之路(1)

最近在重新学习Python这门语言,在网上找到了一个教程感觉挺好的,用这篇Blog写一点自己学习的总结和练习代码。

The link of tutorials: Python

编译器和解释器(Complier and Interpreter)

简单来说计算机不能直接明白我们日常用的语言,编译器和解释器分别是两种可以实现我们让计算机执行所需的要求,也就是让人能读懂的代码转换成Machine Language(二进制代码)

编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快
解释器则是只在执行程序时,才一条一条的解释成机器语言给计算机来执行,所以运行速度是不如编译后的程序运行的快的

其实我们可以把编译器和解释器分别看成自动档和手动档汽车,前者要求更高,执行更快。而后者更为容易和方便。而我们在shell执行.py文件时需提前输入Python或者Python3,就是我们在调用Python的解释器文件。

Python的特性

在编程语言中,我们所熟悉的c,c++都是编译型语言,而Python是一门解释型语言。所以当我们安装好Python之后,可以理解为我们要为运行的程序提供好了解释器。列举一些Python的特点:

  1. 语法相对其他编程语言相对简单,易上手
  2. 面向对象
  3. 模块调用(许多模块不用用户自己编写,直接调用即可)
  4. 脚本功能

    You know what?

    虽然上面说到Python是一门解释型语言,但是教程中却强调Python其实是一门先编译后解释的语言。具体为什么?引用网上的一些资料

    Python代码在首次运行时,它会将Python代码编译成字节码,如果可以的话,它会将这个字节码保存到.pyc文件中,这样下次启动的时候就不会再编译这些代码而是直接解释运行字节码。事实上,这种机制正在模糊解释器和编译器之间的界限,或者说是模糊了解释型语言和编译型语言的界限.
    解释原文

    数据类型(Data Type)

  5. Text type: str
    1
    print("Hello World")
  6. Numeric types: int, float,complex
    1
    2
    3
    4
    5
    6
    #int
    x=5
    #float
    x=15.5
    #complex
    x=2k
  7. Sequence types: list, tuple, range
    1
    2
    3
    4
    5
    6
    #list
    x=["football","soccer"]
    #tuple
    x=("football","soccer")
    #range
    x=range(10)
  8. Mapping type: dict
    1
    2
    #dict
    x={"Name":"Hao","Age":"24"}
  9. Set type: set
    1
    2
    #set
    x={"Macbook","iPhone","Airpods"}
  10. Boolean type: bool
    1
    x=True
  11. Binary types: bytes,bytearray,memoryview
    1
    x=bytes(2)

    数据运算

    基本的数据元算(加减乘除,比较,逻辑)个人觉得比较常见,我比较想说一下成员运算和身份运算
  12. 成员运算
    1
    2
    3
    4
    5
    6
    a=1
    list=[1,2,3,4]
    if(a in list):
    print("yes")
    else:
    break
  13. 身份运算

    is判断两个标识符是不是引用自一个对象

1
2
3
4
5
6
7
8
9
10
11
x=1
y=1
if ( a is b ):
print ("ture")
else:
print ("false")
y=2
if ( id(a) == id(b) ):
print ("true")
else:
print ("false")

Result:
true
false

练习代码

通过编写一个简单的程序,我觉得就可以把基础的语法和数据变量应用到实际中加以练习

编写登陆接口

  • 输入用户名和密码
  • 认证成功后显示信息
  • 输错三次后失败
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import getpass
username="hao808"
password="123456"
count=0
while count<3:
name=input("Username:")
pwd= getpass.getpass("Password:")
if name==username and pwd==password:
print("Weclome!")
break
else:
print("Invaild username or password! Try again!")
count+=1
else:
print("too many attempts!")

编写菜单

  • 多级菜单
  • 可返回,可退出
  • 应用字典和列表
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    menu = {
    'Asia':{
    'China':{
    'Beijing':{},
    'Shanghai':{},
    },
    'Japan':{
    'Toyko'
    },
    },
    'North America':{
    'USA':{
    "Texas":{
    'Belton':{},
    },
    },
    'Canda':{},
    },
    'Europe':{
    'Spain':{
    'Madrid':{},
    },
    },
    }
    current_layer = menu
    #convert dict to list
    layers = [menu]
    while True:
    for k in current_layer:
    print(k)
    choice = input("Choose your option or press (b/q) to back or quit: ").strip()
    if choice == "b":
    current_layer = layers[-1]
    layers.pop()
    if choice=="q":
    break
    elif choice not in current_layer:continue
    else:
    layers.append(current_layer)
    current_layer = current_layer[choice]
  • Code
  • Python

扫一扫,分享到微信

微信分享二维码
Why MacOS?
Python Questions
© 2021 Tianhao Cui
Hexo Theme Yilia by Litten
  • All Blogs
  • My Verse

tag:

  • Code
  • Blockchain
  • Python
  • Others
  • Interview
  • Apple
  • MacOS
  • Algorithm
  • iPhone

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

And now these three remain:faith,hope,love
But the greatest of these is love