python3实例

1、一行代码求一个数的阶乘

例如:求5的阶乘

from functools import reduce
print((lambda k: reduce(int.__mul__, range(1, k+1), 1))(5))

借鉴:https://www.cnblogs.com/QI1125/p/7496129.html

原文地址:https://www.cnblogs.com/yjt1993/p/10364461.html

时间: 2024-08-30 16:15:11

python3实例的相关文章

Python3 实例(二)

Python 判断字符串是否为数字 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字: 实例(Python 3.0+) -- coding: UTF-8 -- Filename : test.py author by : www.runoob.com def is_number(s):try: float(s)return Trueexcept ValueError: pass try: import unicodedata unicodedata.numeric(

Python3 实例

Python 判断字符串是否为数字 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字: 实例(Python 3.0+) # -*- coding: UTF-8 -*-# Filename : test.py# author by : www.runoob.comdef is_number(s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.

Python3 实例(三)

Python 十进制转二进制.八进制.十六进制 以下代码用于实现十进制转二进制.八进制.十六进制: 实例(Python 3.0+) -- coding: UTF-8 -- Filename : test.py author by : www.runoob.com 获取用户输入十进制数 dec = int(input("输入数字:")) print("十进制数为:", dec)print("转换为二进制为:", bin(dec))print(&qu

Python3 实例(八)

Python 归并排序 归并排序(英语:Merge sort,或mergesort),是创建在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 分治法: 分割:递归地把当前序列平均分割成两半. 集成:在保持元素顺序的同时将上一步得到的子序列集成到一起(归并). 实例 def merge(arr, l, m, r): n1 = m - l + 1n2 = r- m # 创建临时数组 L = [0] * (n1) R = [0] * (

Python3 实例(一)

Python Hello World 实例 以下实例为学习Python的第一个实例,即如何输出"Hello World!": 实例 -- coding: UTF-8 -- Filename : helloworld.py author by : www.runoob.com 该实例输出 Hello World! print('Hello World!')执行以上代码输出结果为: Hello World!Python 数字求和 以下实例为通过用户输入两个数字,并计算两个数字之和: 实例(

Python3 实例(五)

Python 翻转列表 定义一个列表,并将它翻转. 例如,对调第一个和第三个元素: 翻转前 : list = [10, 11, 12, 13, 14, 15]翻转后 : [15, 14, 13, 12, 11, 10]实例 1 def Reverse(lst): return [ele for ele in reversed(lst)] lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst)) 以上实例输出结果为: [15, 14, 13, 12, 1

Python3 实例(六)

Python 判断字符串是否存在子字符串 给定一个字符串,然后判断指定的子字符串是否存在于改字符串中. 实例 def check(string, sub_str): if (string.find(sub_str) == -1): print("不存在!") else: print("存在!") string = "www.runoob.com"sub_str ="runoob"check(string, sub_str)执行

django python3 实例 mysql数据库

views.py中代码: def show_all_user(req): num=1 if 'pagenum' in req.POST: num=req.POST['pagenum'] tlist=User.objects.all() p=Paginator(tlist,30) page=p.page(num) return render_to_response('show_all_user.html',{'all_user':page.object_list,'pagenum':num,'to

Python基础之 一 字符编码及转换

python2 / python3编码转换 先上图一张: 说明:python编码转换的流程是 先进行decode解码,然后进行encode编码 解释: u'你好'  -->带u表示为unicode编码 b'\xc4\xe3\xba\xc3'   --> 带b的表示bytes类型由于utf8 是unicode的扩展,所以unicode和utf8之间是可以直接打印 注意:所有decode动作都是将已编码文件解码为unicode,然后在进行其他编码格式转换(通过encode) 直接举例说明:#pyt