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 |

相关推荐
Csvn1 天前
🌟 LangChain 30 天保姆级教程 · Day 13|OutputParser 进阶!让 AI 输出自动转为结构化对象,并支持自动重试!
python·langchain
cch89181 天前
Python主流框架全解析
开发语言·python
sg_knight1 天前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
好运的阿财1 天前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 天前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
小白学大数据1 天前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
Hui Baby1 天前
springboot读取配置文件
后端·python·flask
阿Y加油吧1 天前
回溯法经典难题:N 皇后问题 深度解析 + 二分查找入门(搜索插入位置)
开发语言·python