简单认识 Python(根据个人学习进度,持续跟新中...)

1. 变量命名规则

  1. 字母、数字、下划线,不能有数字开头
  2. 大驼峰写法MyName
  3. 小驼峰写法myName

2. 数据类型

  1. 数字类型:int、float、bool
  2. 非数字类型:str、list(list 列表_esc_newline_[1,2,3,4])、tuple(tuple 元组_esc_newline_(1,2,3,4))、dict(dict 字典_esc_newline_{'name':'jack','age':18})

3. 类型强制转换

int( )

float( )

str( ) 或 直接加 ' '

4. 输入输出

input( )

print( )

5. 格式化输出

%d 填充整型

%f 填充浮点型

%s 填充字符串

name = 'jack'
age = 18
height = 180
num = 90
print('我的名字是%s,我的年龄是%d,我的身高是%.2f,考试及格率是%d%%' %(name,age,height,num))
5.1 字符串格式化
# format的应用
name = 'tom'
height = 1.75
sid = 1
num = 80
print('我的名字是{},我的身高是{:.2f},我的学号是{:03d},我的考试及格率是{}%'.format(name,height,sid,num))

6. 运算符

+-*/ 加减乘除

// 求商

** 求幂

7. 逻辑运算符

and

or

not

8. 判断语句(if elif else)

程序的三大流程(三种结构):

  1. 顺序,代码从上到下全部执行
  2. 分支,判断语句,有选择性的执行
  3. 循环,重复执行某一部分的代码
name = input()
age = input() # 代码有漏洞,仅用于提示类型强转的使用
if name == 'jack' or int(age) > 20:
	print('welcome my brother')
elif name == 'tom':
	print('hello my friend')
else:
	print('Get out!')
score = int(input('请输入成绩:')) # bug:只能输入数字
if score >= 90:
	print('best')
elif score >= 80 and score <90:
	print('good')
elif score >= 60 and score <80:
	print('not bad')
else:
	print('bad')
8.1 随机数工具包
import random
print (random.randint(1,5)) # 输出随机整数1 - 5

9. 循环语句(while)

# 求1 - 100 之间偶数的和
i = 1
sum = 0
while i <= 100:
	if i % 2 == 0:
		sum += i
	i += 1
print(sum)

9.1 循环语句(for...in...)
result = input()
for i in result:
    if i == 'l':
        continue
    print(i)
print('-'*30)
for i in result:
    if i != 'l':
        print(i)

10. 字符串

字符串下标:下标从0开始记起

# 输出第三位,下标为2

str1 = 'abcdefg'
print(str1[2])
print(str1[6])
print(len(str1)) # 字符串长度len

字符串切片:[start : end : step]

str1 = 'abcdefg'
print(str1[0:3:2])

字符串内容查找:字符串.find(sub_str, start, end)

str1 = 'nihao wode mingzi jiao zuo jack hello world'
str2 = str1.find('jack')
print(str2)

11. 列表

# 定义空列表
    my_list1 = []

# 定义列表
    my_list2 = [3.14,'小明','jack',False]
    print(my_list2) # [3.14, '小明', 'jack', False]
    print(my_list2[2]) # jack

# 查找数据的下标 列表.index()
    my_list4 = [3.2,'jack',123,'tom']
    print(my_list4.index(123)) # 2

# 统计数据次数 列表.count()
    my_list5 = [123,'jack',123,'tom']
    print(my_list5.count(123)) # 2

# 添加列表数据
    # 列表尾部添加 列表.append()
    my_list6 = [12,'jack','小明','3ks']
    my_list6.append(3.26523)
    print(my_list6)
    # 指定位置添加 列表.insert(下标,数据)
    my_list7 = [235,'kom','ajc']
    my_list7.insert(1,'world')
    print(my_list7)

# 删除列表数据
    # 根据下表删除 列表.pop(下标)
    my_list7 = [235,'kom','ajc']
    my_list7.pop(2)
    print(my_list7)
    # 根据数据值删除 列表.remove(数据值)
    my_list7 = [235,'kom','ajc']
    my_list7.remove('kom')
    print(my_list7)

# 列表数据颠倒 列表.reserve()
    my_list8 = [1,2,3,4,5]
    my_list8.reverse()
    print(my_list8)

# 列表排序
    # 升序
    my_list9 = [1,2,9,7,5]
    my_list9.sort()
    print(my_list9)
    # 降序
    my_list9.sort(reverse=True)
    print(my_list9)

12. 元组

元组和列表的区别:

元组用 ( ),列表用 [ ]

元组内数据不可改变,列表内数据可以改变

# 定义元组
    my_tuple = ('h','e','l','l','o')
    print(my_tuple)

# 查找数据的下标 元组.index()
    my_tuple = ('h','e','l','l','o')
    print(my_tuple.index('l'))

# 统计数据次数 元组.count(数据)
    my_tuple = ('h','e','l','l','o')
    print(my_tuple.count('l'))

# 判断数据是否存在
    my_tuple = ('h','e','l','l','o')
    print('l' in my_tuple)

13. 函数

# 函数的定义
def 函数名():
    函数中的代码
    函数中的代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值