python基本使用(1)

1. 基本使用

1.1 print

print数字:print(1) 

print函数:print(1+2)      >>>3

print字符串:print("str")   /print('str')   str显示绿色字体

>>> print(1)
1
>>> print(we are)
SyntaxError: invalid syntax
>>> print('we are')
we are
>>> print("'I am'")
'I am'
>>> print("apple"+'4')
	  
apple4
>>> print('apple'+str(4))
	  
apple4
>>> print(1+2)
	  
3
>>> print("1+2")
	  
1+2
>>> print(int("1")+2)
	  
3
>>> print(float('1.3')+3)
	  
4.3

1.2. 基础数学运算

+ - *  /  %(余数)  //(取整)  **(指数乘积)

>>> print(1+2)
	  
3
>>> print("1+2")
	  
1+2
>>> print(int("1")+2)
	  
3
>>> print(float('1.3')+3)
	  
4.3
>>> 2*3
	  
6
>>> 10%4
	  
2
>>> 10//3
	  
3
>>> 2**5
	  
32

1.3. 数值、运算存储到内存中:变量 variable

File ->new file中输入如下

点击 Run -> run module,有如下保存提示

点击确定,

保存完成后,输出如下

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/test1.py 
1 7

2. while 和 for 循环

2.1 while

注:while结尾要有英文的冒号(:),且最后两行要空格(ctrl+【),表示是在while循环中的语句,若写成如下形式

则有提示

若写成

则会循环输出1

正确写法应该是

输出为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
1
2
3
4
5
6
7
8
9

集合类型:有list、tuple、dict、set等

tuple类型

输出为

 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
python
2.7
64

dictionary类型

结果为

 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
lan pylon
version 3.6
platform 64

注:这里的key输出和插入的顺序不一定保持一致。

set类型

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
4
1
2

set集合会去除重复项。输出的结果也不一定按照输入的顺序。

迭代器、生成器

参考https://morvanzhou.github.io/tutorials/python-basic/basic/03-2-for/

2.2 for循环

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
1
2
5
7
19

对于for循环,很有用的一个内部函数range(电脑自动生成的一个迭代器)

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
1
2
3
4
5
6

range的三种用法

一:range(start,stop)

二:range(stop),默认从0开始

三:range(start,stop,step) 依次增加 step 的值

3. if判断

有if、if else、if elif

if

if else

if elif

4. 定义功能

4.1 def函数

输出

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
this is a function
3

函数参数

输出

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/2.py 
this is a function
20

4.2 def默认函数参数

输出

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/1.py 
price: 1000 color: blue brand: carmy

即:函数声明在需要默认参数的地方用 = 号给定即可,注意:所有的默认参数都不能出现在非默认参数的前面,即上面例子中,有默认参数的 color、brand 不能放在没有默认参数的 price 前面。否则会报错,如下

4.3 可变参数

传入的参数可以变化的,可以将这些参数封装成一个 list 或 tuple 传入,注意可变参数在函数定义中不能出现在特定参数和默认参数前面,因为可变参数会吞噬掉这些参数。

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/1.py 
lily total grand is: 12

上面例子中,可变参数为 *grand,用 * 修饰,表明参数是一个可变参数。

4.4 关键字参数

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/1.py 
name is: lily
age 12
math 90
hobby computer

上面例子中,关键字参数 **kw 用 ** 修饰,表明该参数是关键字参数,通常关键字参数放在函数参数列表的最后。

5. 变量形式

局部&全局变量

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/1.py 
A= 10
b pass = 5
25
b now= 20

若在函数 func()中未以 global 形式定义 b,则 b now 的值不改变,但是在函数返回值中 b 取决于函数中 b 给定的值。即

结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/1.py 
A= 10
b pass = 5
25
b now= 5

6. 外部模块安装

 外部模块用于 import 脚本时用到,如Numpy(多维数组处理)、matplotlib(绘图库)、scipy(科学计算)等模块,不是python自带模块,需要安装。

window安装网址:

Numpy:https://sourceforge.net/projects/numpy/files/

matplotlib:https://sourceforge.net/projects/matplotlib/

scipy:https://sourceforge.net/projects/scipy/

如 numpy 下载

选择版本进入

linux:

终端输入:pip3 install 模块名(pip3是python3+下,python2+下用pip)

linux下更新模块:pip3 install -U 模块名

7. 读写文件

7.1主要内容 \n  file.open \t

则在上面3.py同一目录下会生成文件 my file.txt,且内容如下

7.2 给文件增加内容

出错 发生系统返回错误(这里后续学习补充)

7.3 文件读取

8. class类

8.1 定义class与定义func一样,一般第一个字母大写,类内函数的self表示属于此类

结果

>>> cal=Calculator()
>>> cal.name
'math calculator'
>>> cal.price
200
>>> cal.add(18,7)
25
>>> cal.minus(16,8)
8

第一行表示用 cal 代替类名 Calculator。

8.2 class类的init功能

 __init__可以理解为初始化class的变量

结果如下

>>> c=Calculator('plot',20, 30, 40, 12)
>>> c.name
'math calculator'
>>> c.price
20
>>> c.h
30
>>> c.wi
40
>>> c.we
12
>>> c.add(5,6)
11

9. input输入

输出结果为

>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/3.py 
please give a number:1
this is a good number
>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/3.py 
please give a number:2
this is not a good number
>>> 
 RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/3.py 
please give a number:3
the number is: 3

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值