Python Basics

本文详细介绍了Python的基础知识,包括解释器与编译器的区别,变量与保留字,有效语法模式,赋值语句,运算优先级,不同类型的数据处理,以及输入输出操作。特别讨论了Python中的赋值方式,如级联赋值、同时赋值和增量赋值,并举例说明了不同类型的除法和数据类型转换。此外,还提到了字符串操作和使用eval()函数进行表达式求值。
摘要由CSDN通过智能技术生成

python 知识点整理(一)

本文只是对python部分知识点进行学习和整理
本篇主要是针对python的基本信息的总结

interpreter and compiler 解释器和编译器

compiler: C/C++ (一种代码语言到另一种代码语言 再到平台进行运行操作 产物为 [另一种代码])
interpreter: Python (直接进行相关的代码操作 产物为 [运行结果]

elements of python

variables/reserved words

存放在memory的一个命名空间 可以存储数据并被修改数据

valid syntax patterns语法模式

program -interactive/script

python flow (mainly in part2)

  • sequential flow
  • conditional flow -if /elif/else
  • repeated flow

constants & variable

constants

  • fixed values
  • string constants use the (“”) or (‘’)

rules for defining variables in Python

部分名字不可以被称作变量名:
eg: False True None and as assert break ···

statement /assignment

assignment statement

can be retrieved from located memory 值会被改变

x=10
print(x)
x=100-10+x*3-x/10
print(x)

cascaded assignment 级联

multiple variables can be set as the same value by using single assignment statement

z=y=x=2+7+2
x,y,z

simultaneous assignment

value of two variables can be changed

a="deepsecret"
b="you'll never guess"
b,a=a,b #exchange the vari
c=12
d=34
d,c=c,d#exchange

augmented assignment 增量赋值

x+=1 等价于 x=x+1
x-=2x7 等价于 x=x-(2x7)

x1=22
x1+=7

order evaluation (operator precedence)运算优先级

  • highest to lowest
    • parenthesis are always with highest priority ()最高优先级
    • power **乘积
    • multiplication division remainder 乘除余
    • addition subtraction 加减
    • left to right

division : floor division&divmod

  • three different kinds of division:
    • floor division(//)
    • divition
    • divmod()

for the floor division, pay attention to the data type

#floor division
print(143//25)
print(143.4//25)#float
print(9//2.5)#float
5
5.0
3.0
#int
print(256//10)
#float
print(256/10)
#商&余数
divmod(143,25)#5,18
25
25.6
(5, 18)

data type

type matters

  • Some operations are prohibited on certain types
  • cannot ‘add1’ to the string
  • check the type by type()
  • type of variable can be changed and will be determined by the value that is last assigned to the variable

type conversion

  • Use int() & float() to convert other data type to integer & float
  • convert numbers into strin using function str()

string operations

  • ‘+’ 连接
  • '*'多项连接
str1='abc'
str2='def'
str3='hij'
str1+str2+str3
'abcdefhij'

input & output

input():get a string

eval()

返回传入字符串的表达式的结果。就是说:将字符串当成有效的表达式 来求值 并 返回计算结果。

eval函数就是实现list、dict、tuple与str之间的转化,同样str函数把list,dict,tuple转为为字符串

string="12+7"
print(string)
print(eval(string))
print(string,"=",eval(string))
12+7
19
12+7 = 19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值