Python基础知识

交互式解释器

启动python后,可看到类似下面的提示符

C:\Windows\system32>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

核实是否能用,输入后回车可以看到输出

>>> print("hello,world!")
hello,world!
>>>

其他语言往往后边加一个,如果愿意加可以加,通常都不这么做。

算法是什么

算法是一系列解决问题的清晰指令,也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。

数和表达式

examples:

>>> 2+2
4
>>>

除法运算结果为小数,浮点型

>>> 1/2
0.5
>>> 2/3
0.6666666666666666
>>>

如果想丢掉小数部分可以使用双斜杠

>>> 1//2
0
>>> 2//3
0
>>> 3//2
1
>>>

求余(求模)运算

>>> 10%3
1
>>> 3%3
0
>>> 10//3
3
>>> 10%3
1
>>> 9//3
3
>>> 9%3
0
>>> 2.75%0.5
0.25
>>>

在这里10//3,结果向下取整,3x3=9,余1。
9除3结果正好为3,余数为0。

求余运算也可以用于浮点数。可用于负数。
examples:

>>> 10%3
1
>>> 10% -3
-2
>>> -10% 3
2
>>> -10% -3
-1
>>>

对于除法运算,重点是向下取整运算结果。因此在结果为负数的情况下,取整后将离0更远。

乘方(求幂)运算

>>> 2 ** 3
8
>>> 3**2
9
>>> -3**2
-9
>>> (-3) ** 2
9
>>>

变量

变量是表示特定值的名称。例如用名称x来表示3

>>> x=3
>>>

这称之为赋值,将值3赋给了变量x。给变量赋值后,就可以在表达式中使用它。

>>> x * 2
6
>>>

使用python变量前必须给他赋值,因为python变量没有默认值。

语句

表达式是一些“东西”,语句是做一些事情。

获取用户输入

函数input()

>>> input("the meaning of life:")
the meaning of life:24
'24'
>>>

初识if语句
条件不满足什么都不做,条件满足时,将执行冒号后边的语句。

if 1==2:print('1=2')
if 1==1:print('1=1')

函数

函数就像时小型的程序,可以来执行特定的操作。python内置了很多函数,可以用来完成很多神奇的任务。

print(pow(2,3))
8

Process finished with exit code 0

可以结合使用函数调用和运算符来编写更复杂的表达式

print(10+pow(2,2*2)/3)

15.333333333333332

Process finished with exit code 0

abs计算绝对值;round将浮点数圆整为与之接近的整数

模块

可将模块视为扩展,通过将其导入可以扩展python功能。导入模块,可使用特殊命令import。

floor向下圆整
ceil向上圆整

import math
print(math.floor(32.9))
print(math.ceil(32.9))
32
33

Process finished with exit code 0

如果确定不会从不同模块导入多个同名函数。你可能不想每次调用函数时都指定模块名。在这种情况下,可以使用命令:

from math import sqrt
print(sqrt(9))

3.0

Process finished with exit code 0

cmath和复数

函数sqrt可用于计算平方根。

from math import sqrt
print(sqrt(-1))
Traceback (most recent call last):
  File "D:/PythonProjects/python3JCJC/from.py", line 2, in <module>
    print(sqrt(-1))
ValueError: math domain error

Process finished with exit code 1

如果坚持将值域限定为实数,并使用近似的浮点数实现,就无法计算负数的平方根。负数的平方根为虚数,而由实部和虚部组成的数为复数。python提供了一个专门用于处理复数的模块cmath

import cmath
print(cmath.sqrt(-1))

1j

Process finished with exit code 0

虚数以j或J结尾。python本身提供了对复数的支持

保存并执行程序

编写简单语句保存

name=input("what's your name: \n")
print("welcome,"+ name + "!")

运行

what's your name: 
wzw
welcome,wzw!

命令提示行运行python文件

D:\PythonProjects\python3JCJC>python "what's your name.py"
what's your name:
wzw
welcome,wzw!

D:\PythonProjects\python3JCJC>

注释

python中# 比较特殊:在代码中#号以后到行尾的所有内容都将被忽略。

# 计算圆的周长
import math
pi = 3.14
r = 5
print(2 * pi * r )
31.400000000000002

Process finished with exit code 0

字符串

字符串的用途众多,主要用途是表示一段文本。

单引号字符串以及引号转义

与数一样,字符串也是值:
打印字符串时,用单引号将其括起来,使用双引号将其括起来。其实没有什么差别。

print("hello,world")
print('hello,world')
print('"hello, world"')
print("'hello, world'")
print("let's go!")
print('"hello,world!" she said')
print('let\'s go!')#使用反斜杠转义
print("\"hello,world!\" she said")
hello,world
hello,world
"hello, world"
'hello, world'
let's go!
"hello,world!" she said
let's go!
"hello,world!" she said

Process finished with exit code 0

拼接字符串

print("let's say" '"hello,world!"')
x= "hello,"
y= "world"
print(x+y)

let's say"hello,world!"
hello,world

Process finished with exit code 0

字符串表示str和repr

通过两种不同的机制将值转换成了字符串。使用str能够以合理的方式将值转换为用户能够看懂的字符串。使用repr时,通常会获得值的合法python表达式表示。

print(repr("hello,\nwordld"))
print(str("hello,\nworld"))

'hello,\nwordld'
hello,
world

Process finished with exit code 0

长字符串、原始字符串和字节

长字符串

表示很长的字符串,可以使用三引号。

print('''this is a very long string. it continues here''')

this is a very long string. it continues here

原始字符串

print('hello,\nworld')
print('C:\nowhere')
print('C:\\nowhere')
print('C:\\program files\\fnord\\bar\\faed')
print(r'C:\program files\fnord\bar\faed')
hello,
world
C:
owhere
C:\nowhere
C:\program files\fnord\bar\faed
C:\program files\fnord\bar\faed

通过上面的输出,在这种情况下,原始字符可以派上用场了。原始字符用前缀r表示。

原始字符串不能以单个反斜杠结尾。

Unicode、Bytes和bytearray

每个Unicode字符都用一个码点表示,而码点是Unicode标准给每个字符指定的数字;计算机键盘不可能包含几十万个按键,因此有一种指定Unicode名称机制;使用16位或32位的十六进制字面量或者使用字符的Unicode名称。

print("\u00c6")
print("\U0001F60A")
Æ
😊

小结

学到的主要内容有以下
算法:
表达式:
变量:
语句:
函数:
模块:
程序:
字符串

思维导图

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Energet!c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值