学习python的基础语法_python 学习_基础语法

python 基础语法

1变量

声明变量

name = "augustyang"

变量定义规则

1) 变量名只能是字母,数字或下滑下的任意组合

2) 变量名的第一个字符不能是数字

3) 关键字不能声明为变量名

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

定义方式:

1)驼峰方式

AgeOfOldboy = 56

2)下划线方式

age_of_oldboy = 56

2.常量

常量指不变的量 如 π 3.1415926....

3. 注释

#

""" """ ''' '''

4.数据类型

① 数字

1)int(整型)

2)long(长整型)

在Python3里不再有long类型了,全都是int

②字符串

在Python中,加引号的字符都是被认为是字符串

有单引号 双引号 多引号 他们之间没有区别

③ 布尔型(bool)

布尔类型很简单,就两个值, 一个True(真)一个False(假),主要用着逻辑判断

5.列表类型

#创建列表

test = [1,2,3,4]print(test)

[1, 2, 3, 4]

#访问列表

test = [1,2,3,4]print (test[1])2

# 显示第二位到最后

print(test[1:])

[2, 3, 4]

#倒序

test = [1,2,3,4]print(test[::-1])

[4, 3, 2, 1]

#访问最后一个列表

test = [1,2,3,4]print(test[-1])4

#添加字段

test = [1,2,3,4]

test.append(33)print(test)

[1, 2, 3, 4, 33]

# 2个列表合并成一个列表extend

new_test = [22,33]

test.extend(new_test)print(test)

[1, 2, 3, 4, 0, 22, 33]

# append

new_test = [22,33]

test.append(new_test)print(test)

[1, 2, 3, 4, 0, [22, 33]]

#insert

test = [1,2,3,4]

test.insert(2,222)print(test)

[1, 2, 222, 3, 4]

# pop 没有指定参数 就删除最后一个参数 指定参数, 删除指定参数

test = [1,2,3,4]print(test.pop())4

# remove

test = [1,2,3,4]

test.remove(1)print(test)

[2, 3, 4]

# reverse 翻转

test = [1,2,3,4]

test.reverse()print(test)

# 排序

test = [1,2,33,6,3,4]

test.sort()print (test)

# 返回列表中关键字的个数

test = [1,2,33,6,3,4]print(test.count(1))1

6.字典类型

7.运算符

① 算数运算

运算符

描述

实例

+

加      两个对象相加

2+3      输出结果5

-

减     一个对象减去另一个对象

7-6                         1

*

10*10                     100

/

5/2                         2.5

%

取模    返回除法的余数

5%2           取返回余数部分1

**

幂      返回x的y次幂

2**2                        4

//

取整数  返回商的整数部分

9//2            输出4  9.0//2.0输出4.0

② 比较运算

运算符

描述

实例

==

等于        比较对象是否等于

(a==b)              等于就返回True

!=

不等于      比较对象是否不等于

(a!=b)              不等于就返回True

>

大于

(a>b)               大于就返回True

<

小于

(a

>=

大于等于

(a>=b)              大于等于就返回True

<=

小于等于

(a<=b)              小于等于就返回True

③ 赋值运算

运算符

描述

实例

=

赋值运算符

c=a+b 将a+b的运算结果赋值为c

+=

加法赋值运算符

c+=a       等于c=c+a

-=

减法赋值运算符

c -=a                           c=c-a

*=

乘法赋值运算符

c*=a                           c=c*a

/=

除法赋值运算符

c/=a                           c=c/a

%=

取模赋值运算符

c%=a                           c=c%a

**=

幂赋值运算符

c**=a                          c=c**a

//=

取整除赋值运算符

c//=a                           c=c//a

④逻辑运算

运算符

描述

实例

and

or

not

8.流程控制

if...else

单分支

count = 10

if count > 5:print ('ok')#结果 ok

双分支

count = 10

if count > 11:print ('ok')else:print ("no")#结果 no

多分支

count = 60

if count >= 80:print ('优秀')elif count >= 60:print("良好")else:print ("不及格")#结果 良好

while 循环

count =0while count < 3:print(count )

count+=1

#输出结果

"""0

1

2"""

语法

while  条件:

执行代码

特殊语法

while ... else ..

#当while循环正常执行完,中间没有被break中止的话,就会执行else后面的语句

count =0while count < 3:print(count )

count+=1

else:print("--------")"""0

1

2

--------"""

count =0while count < 3:print(count )

count+=1

if count == 2:break

else:print("--------")"""0

1"""

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值