Python From Zero to Hero(一)- Quickstart & Variable

"Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to view the details of the event ."

一、Python Introduction

Python is a programming language that lets you work quicklyand integrate systems more effectively

Python is a high-level scripting language that combines interpreted, compiled, interactive, and object-oriented scripts.

Python is designed to be highly readable. Compared with other languages, English keywords are often used, and some punctuation marks in other languages. It has a more distinctive grammatical structure than other languages.

Python is an interpreted language : this means that there is no compilation part of the development process. Similar to PHP and Perl languages.

Python is an interactive language : that is, code can be executed directly from a Python prompt  >>> .

Python is an object-oriented language : this means that Python supports an object-oriented style or a programming technique in which code is encapsulated in objects.

Python is a language for beginners : Python is a great language for beginner programmers, supporting a wide range of application development, from simple word processing to WWW browsers/websites to games.

Features of Python

1. Easy to learn : Python has relatively few keywords, simple structure, and a well-defined syntax, making it easier to learn.

2. Ease of reading : Python code is defined more clearly.

3.易于维护:Python的成功在于它的源代码是相当容易维护的。

4.一个广泛的标准库:Python的最大的优势之一是丰富的库,跨平台的,在UNIX,Windows和Macintosh兼容很好。

5.互动模式:互动模式的支持,您可以从终端输入执行代码并获得结果的语言,互动的测试和调试代码片断。

6.可移植:基于其开放源代码的特性,Python已经被移植(也就是使其工作)到许多平台。

7.可扩展:如果你需要一段运行很快的关键代码,或者是想要编写一些不愿开放的算法,你可以使用C或C++完成那部分程序,然后从你的Python程序中调用。

8.数据库:Python提供所有主要的商业数据库的接口。

9.GUI编程:Python支持GUI可以创建和移植到许多系统调用。

10.可嵌入: 你可以将Python嵌入到C/C++程序,让你的程序的用户获得"脚本化"的能力。

Python 脚本的格式

image.png

Python 脚本内部的结构

Python脚本文件的结构如下

  • 头部注释区域
  • 导入区域
  • 代码区域
    • 代码的执行顺序
    • 代码的注释
  • 代码执行入口

image.png

Python 脚本的执行

Python脚本的执行命令如下

image.png

也可以在Pycharm中执行,鼠标选中要执行的文件,点击Run即可

image.png 也可以在Pycharm中打开Terminal终端执行

image.png

Python 导入

导入是将Python的一些功能函数放到当前的脚本中使用的一种操作,除了Python内置函数外,任何其他函数都需要import到当前脚本中才能使用

导入使用import关键字 + 模块名的方式导入功能函数或者模块

在python_heros中新建import_sample.py脚本

import os
import time

print("当前目录位置为:%s"%os.getcwd())
print("当前时间戳为:%s"%time.time())
复制代码

image.png

Python中程序的执行顺序

Python中的代码是自上而下、逐行执行的

Python的内置函数

print 函数

print函数的作用是将信息在控制台进行打印

print("这是需要在控制台显示的信息")
复制代码

image.png

input 函数

input函数可以 接收一个标准输入数据,返回string类型数据,也就是说执行input函数后可以在命令行输入一行信息,input函数会将这行信息返回成字符串

image.png

Python 代码中的注释

注释是Python代码中不会被Python直接执行的语句 注释的形式有#、"""、'''三种形式

  • #:# 之后的内容都是注释内容
  • 三引号:"""被三引号包裹的内容是注释内容,可以随意换行"""
  • 单引号:''''被单引号包裹的内容是注释内容,与三引号注释一样,可以随意换行'''

Python 脚本的执行入口

一般将代码执行的入口叫做主函数main, Python中函数的名字可以用__name__来表示

if __name__ == '__main__':
    # 要执行的代码
复制代码

上面这行代码就是判断函数的名字是不是等于__main__,如果是那么就执行下面的代码。 Python脚本的名字和函数的名字有什么区别? 这里举一个不是很恰当的例子,假设一个名字为print_sample.py的脚本,对于IDE来说脚本的名字叫做print_sample,既

__name__ == 'print_sample'
复制代码

而对于脚本本身来说就是__main__,既

__name__ == '__main__'
复制代码

Python中使用缩进来表示代码块

创建一个脚本main_sample.py,练习main函数

入口函数是整个脚本的末尾

import time

if __name__ == '__main__':
    print(time.time())
复制代码

Python 中并不是一定需要入口函数

二、Python 中的变量

变量是一个容器,用来存储数据,存储的数据是可以变化的。变量存在于内存中,当变量被定义后就会被存入内存当中

变量名的命名规则

  • 变量名的组成:必须是数字、字母、下划线组成
  • 变量名的长度:任何长度,但是建议20字符以内
  • Requirements for variable names: variable names must start with a letter, not a number, and variable names are case-sensitive

When the variable name consists of multiple words, it is not recommended to use camel case, and it is recommended to use underscore.

image.pngYou can also define multiple variables in one line for quick definition

image.png

3. Keywords in Python

Keywords in Python refer to special words built into Python for processing business logic. Keywords cannot be used in the naming of custom variables.

The difference between variable names and keywords is that variable names are used to assign values ​​to variables, and keywords are used for business processing.

Common Keywords in Python

As long as a strong keyword is defined as a variable, the compiler will report an error. If a weak keyword is defined as a variable, the compiler will not report an error, but the function of the keyword will be lost.

Some common keywords in Python:

  • Built-in constants: False, None, True
  • Logical AND or NOT: and, or, not
  • 判断:if...elif...else、in、is
  • 循环:for、while、break、continue
  • Import: import, from
  • 函数:def、lambda、pass、return、yield
  • Exception handling: try...except...finally, raise
  • Rename: as
  • Variable scope: global, nonlocal
  • class: class
  • delete: del
  • Context management: with

Guess you like

Origin juejin.im/post/7079371576542429191