Basic knowledge of Python

The overall introduction is here

Introduction to Python

1. Variable Definition

You needn't assign a type to a variable, because Python interpreter will automatically judge the type. You needn't declare a variable before define. The following is an example of how to state a variable:

python 复制代码
a = 1

b = "hello"

c = 3.14159

As you can see, definition of variable is simple, just use = .

The name of variable shoule make up letters, $, numbers, and _ . A variable mustn't begin with a number.

Note that in the instance above, a,b and c is the reference of the variable. That is, 1 "hello" and 3.14159 is in the heap memory and a,b and c just their alias. It is important.

2. Comment

Comment is important for us to understand the function of our code. We use # to indicate the line is a comment.

python 复制代码
# This is a comment
# The comment will be ignored by the interpreter.

a = 1 # You can also add a comment here, too.
b = "hello"
c = 3.14159

3. Type of variable

We needn't declare the type of a variable, but it doesn't mean that a variable hasn't a type.

There are many types in Python:

|----------|-----------------------------------------------------------|-------------|
| int | integer | a = 10 |
| float | folat number | a = 1.2 |
| complex | complex number | a = 1 + 2j |
| bool | True or False | a = False |
| str | a string of charactors | a = "hello" |
| Sequence | a special type of variable which we will explore it later | |
| None | indicates that a variable is empty | a = None |

When you define a variable, the interpreter will judge the type of the variable automatically.

4. build-in functions

We haven't explore what is function now, so if you can't understand what functions are, you can just memorize the form and use it. You will definitely understand it later.

|---------|---------------------------------------------------------------------------------------------------------------|--------------------------|
| print() | a powerful function which can print out content to your terminal, which can receive almost any type of input. | print(1) |
| | | print("hello") |
| | | print(...) |
| len() | It will return the "length" of an object. The "length" is defined by the object. We will talk about it later. | len([1,2,1]) #return 3 |

相关推荐
纤纡.11 分钟前
PyTorch 入门精讲:从框架选择到 MNIST 手写数字识别实战
人工智能·pytorch·python
kjkdd23 分钟前
6.1 核心组件(Agent)
python·ai·语言模型·langchain·ai编程
小镇敲码人29 分钟前
剖析CANN框架中Samples仓库:从示例到实战的AI开发指南
c++·人工智能·python·华为·acl·cann
萧鼎31 分钟前
Python 包管理的“超音速”革命:全面上手 uv 工具链
开发语言·python·uv
alvin_20051 小时前
python之OpenGL应用(二)Hello Triangle
python·opengl
铁蛋AI编程实战1 小时前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
jiang_changsheng1 小时前
RTX 2080 Ti魔改22GB显卡的最优解ComfyUI教程
python·comfyui
0思必得02 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
沈浩(种子思维作者)2 小时前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算
2301_790300962 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python