Skip to content

Commit

Permalink
'learn-python'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuizhongyi committed May 7, 2020
1 parent 703b52f commit 60a18de
Show file tree
Hide file tree
Showing 94 changed files with 4,789 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 从零学Python-掘金活动/day01/helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Hello World
author: gxcuizy
date: 2018-10-15
"""

import random

# 程序主入口
if __name__ == '__main__':
# 打印
print('Hello World!')

# 取4个随机数
i = 0
rand_list = []
while i < 4:
rand_num = random.randint(0, 499)
if rand_num not in rand_list:
rand_list.append(rand_num)
i += 1
# 输出组队随机编码
print(rand_list)
42 changes: 42 additions & 0 deletions 从零学Python-掘金活动/day02/calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
简易计算器的实现
author: gxcuizy
date: 2018-10-16
"""

# 程序主入口
if __name__ == '__main__':
# 运算方式
operate_type = input('请选择运算类型:1加法,2减法,3乘法,4除法:')
# 判断类型是否有效
while int(operate_type) not in (1, 2, 3, 4):
print('类型选择错误,只能为1-4')
operate_type = input('请重新选择运算类型:1加法,2减法,3乘法,4除法:')
# 第一个数
number_one = int(input('请输入第一个数:'))
# 第二个数
number_two = int(input('请输入第二个数:'))
if operate_type == '1':
# 1加法
result = number_one + number_two
print('加法运算,结果是:' + str(result))
elif operate_type == '2':
# 2减法
result = number_one - number_two
print('减法运算,结果是:' + str(result))
elif operate_type == '3':
# 3乘法
result = number_one * number_two
print('乘法运算,结果是:' + str(result))
elif operate_type == '4':
# 4除法
if number_two == 0:
print('第二个数即除数不能为0')
else:
result = number_one / number_two
print('除法运算,结果是:' + str(result))
else:
print('算法类型错误')
13 changes: 13 additions & 0 deletions 从零学Python-掘金活动/day02/ide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
变量赋值和输出
author: gxcuizy
date: 2018-10-16
"""

# 程序主入口
if __name__ == '__main__':
company, date, *others = ['juejin', '20181016', 'morning', 'Tues']
print(company, date, others)
43 changes: 43 additions & 0 deletions 从零学Python-掘金活动/day03/circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
输入圆长,计算圆周长和面积
author: gxcuizy
date: 2018-10-17
"""

# 程序主入口
if __name__ == '__main__':
# 运算符的用法
number_one = input('请输入第一个数:')
number_two = input('请输入第二个数:')
# + 加法的使用
sum_value = float(number_one) + float(number_two)
print('加法运算:' + number_one + ' + ' + number_two + ' = ' + str(sum_value))
# - 减法的使用
diff_value = float(number_one) - float(number_two)
print('减法运算:' + number_one + ' - ' + number_two + ' = ' + str(diff_value))
# * 乘法的使用
mul_val = float(number_one) * float(number_two)
print('乘法运算:' + number_one + ' * ' + number_two + ' = ' + str(mul_val))
# / 除法的使用
if number_two == '0':
print('除法运算,除数不能为0')
else:
div_value = float(number_one) / float(number_two)
print('除法运算:' + number_one + ' / ' + number_two + ' = ' + str(div_value))
# % 取模的使用
after_val = float(number_one) % float(number_two)
print('取模运算:' + number_one + ' % ' + number_two + ' = ' + str(after_val))

# π的值
pai = 3.14
# 圆长
circle_len = int(input('请输入圆长计算周长和面积:'))
# 计算圆周长
circumference = 2 * pai * (circle_len / 2)
print('圆周长为:' + str(circumference))
# 计算圆面积
area = pai * (circle_len / 2) ** 2
print('圆面积为:' + str(area))
40 changes: 40 additions & 0 deletions 从零学Python-掘金活动/day03/data_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
数据类型和变量的基础使用
author: gxcuizy
date: 2018-10-17
"""

# 程序主入口
if __name__ == '__main__':
# 整型
int_value = 666
print(int_value)

# 浮点数
float_value = 6.6666
print(float_value)

# 字符串
str_value = 'hello world!'
print(str_value)
# 转移字符串
str_escape_value = '这是转义的\'hello world!\''
print(str_escape_value)

# 布尔值
# 布尔值只有True、False两种值
bool_true_value = True
print(bool_true_value)
bool_false_value = False
print(bool_false_value)

# 空值
none_value = None
print(none_value)

# 常量-变量名全部大写
CONS_VALUE = '大表锅'
print(CONS_VALUE)
51 changes: 51 additions & 0 deletions 从零学Python-掘金活动/day03/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
列表的基础语法使用
author: gxcuizy
date: 2018-10-17
"""

# 程序主入口
if __name__ == '__main__':
# 列表的定义
team_list = ['da_biao_guo', 'xiao_biao_mei', 'team']
print(team_list)

# 用len()输出列表长度
list_len = len(team_list)
print(list_len)

# 访问列表的元素值
# 访问第1个元素
print(team_list[0])
# 访问第3个元素
print(team_list[2])
# 访问倒数第2个数
print(team_list[-2])

# 追加元素至末尾
team_list.append('python')
print(team_list)

# 插入元素到指定的位置
team_list.insert(3, 'learning')
print(team_list)

# 删除末尾的一个元素
team_list.pop()
print(team_list)

# 删除指定位置的一个元素
team_list.pop(2)
print(team_list)

# 更新某个元素值
team_list[1] = '小表妹'
print(team_list)

# 列表元素值类型,可以不同
team_list[0] = ['da', 'biao', 'guo']
team_list[2] = 666
print(team_list)
45 changes: 45 additions & 0 deletions 从零学Python-掘金活动/day03/str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
字符串的基础语法使用
author: gxcuizy
date: 2018-10-17
"""

# 程序主入口
if __name__ == '__main__':
# 定义一个字符串
str_value = '这是字符串string'
print(str_value)

# 字符串utf-8编码
str_encode = str_value.encode('utf-8')
print(str_encode)

# 字符串utf-8解码
str_decode = str_encode.decode('utf-8')
print(str_decode)

# 统计字符串的长度
str_len = len(str_value)
print(str_len)

# 格式化字符串
str_format = '这是字符串格式化: %s' % str_value
print(str_format)
# 格式化整数
int_value = 666
int_format = '这是整数格式化: %d' % int_value
print(int_format)
# 格式化浮点数
float_value = 666.66
float_format = '这是浮点数格式化: %f' % float_value
print(float_format)
# 综合使用
format_value = '%s %d + %f 的值' % ('计算', 6, 6.66)
print(format_value)

# 格式化字符串format()的用法
format_fun_value = '{0}的{1}成绩是{2}分'.format('小明', '数学', 99)
print(format_fun_value)
30 changes: 30 additions & 0 deletions 从零学Python-掘金活动/day03/tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
元组的基础语法使用
author: gxcuizy
date: 2018-10-17
"""

# 程序主入口
if __name__ == '__main__':
# 定义一个元组
team_tuple = ('da_biao_guo', 'xiao_biao_mei', 'team')
print(team_tuple)

# 访问元素值
# 访问第1个元素
print(team_tuple[0])
# 访问第3个元素
print(team_tuple[2])
# 访问倒数第2个元素
print(team_tuple[-2])

# 定义一个元素值的元组,必须加一个逗号,
best_language = ('python',)
print(best_language)

# 元组长度同样用len()
tuple_len = len(team_tuple)
print(tuple_len)
80 changes: 80 additions & 0 deletions 从零学Python-掘金活动/day04/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
字典的基础使用和练习
author: gxcuizy
date: 2018-10-18
"""

# 程序主入口
if __name__ == '__main__':
# 定义一个空字典
dict_none = {}
print(dict_none)

# 定义一个非空字典
score_dict = {'math': 96, 'english': 97, 'chinese': 98}
print(score_dict)

# 使用使用dict()创建字典
tuple_math = ('math', '96')
tuple_english = ('english', '97')
tuple_chinese = ('chinese', '98')
dict_a = dict([tuple_math, tuple_english, tuple_chinese])
print(dict_a)

# 使用zip()合并两个列表分别作为字典的key和value
list_key = ['math', 'english', 'chinese']
list_value = [96, 97, 98]
score = dict(zip(list_key, list_value))
print(score)

# 读取字典的value
print(score_dict['math'])

# 修改字典的value
score_dict['chinese'] = 100
print(score_dict)

# keys()获取字典所有的key
dict_key = score_dict.keys()
print(dict_key)

# values()获取字典所有的value
dict_value = score_dict.values()
print(dict_value)

# 使用get()获取key值对应的value
math_value = score_dict.get('math')
print(math_value)

# in 和 not in 判断key在字典中是否存在
print('math' in score_dict)
print('history' not in score_dict)

# 使用items()把字典的对应的key和value组成一个元组返回一个列表
score_list = score_dict.items()
print(score_list)

# 使用copy()复制一个字典
score_copy = score_dict.copy()
print(score_copy)

# 使用clear()清空字典所有元素
score_copy.clear()
print(score_copy)

# pop()删除一个key对应的元素,key存在,返回对应的value,可以指定不存在时的默认返回值
pop_result = score_dict.pop('english')
print(pop_result)
pop_result = score_dict.pop('history', '不存在')
print(pop_result)

# 使用update()更新字典,也就是追加元素的意思
score_dict.update({'history': 95})
print(score_dict)

# 使用fromkyes()创建一个新的字典,key来自序列,value来自自定义(默认为None)
score_new = score_copy.fromkeys([11, 22, 33, 44], 100)
print(score_new)
Loading

0 comments on commit 60a18de

Please sign in to comment.