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. 数据类型
数字
1int(整型)

2long(长整型)
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

**

幂      返回xy次幂

2**2                        4  

//

取整数  返回商的整数部分

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

比较运算

运算符

描述

实例

==

等于        比较对象是否等于

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

!=

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

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

>

大于     

(a>b)               大于就返回True

<

小于

(a<b)               小于就返回True

>=

大于等于

(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 = 0
while count < 3:
    print (count )
    count +=1
    
    
# 输出结果
"""
0
1
2
"""

 

语法
while  条件 :
  执行代码

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

count = 0
while count < 3:
    print (count )
    count +=1
else:
    print("--------")

"""
0
1
2
--------
"""
count = 0
while count < 3:
    print (count )
    count +=1
    if count == 2:
        break
else:
    print("--------")

"""
0
1

"""
原文地址:https://www.cnblogs.com/augustyang/p/10407252.html