1def fact(n):
2if n==1:
3return 1
4return n*fact(n-1)
56 n=int(raw_input("Please Input The Number of N: "))
7print fact(n) 阶乘' ref='nofollow'>Python练习3->阶乘原文:http://www.cnblogs.com/Curious-Python/p/4540150.html
#!/usr/bin/env python
#-*- coding:utf-8 -*-‘‘‘ 统计系统内存信息 ‘‘‘ with open(‘/proc/meminfo‘) as fd:for line in fd:if line.startswith(‘MemTotal‘):MemTotal = line.split()[1]continueif line.startswith(‘MemFree‘):MemFree = line.split()[1]breakprint"总内存:%sM" % (int(MemTotal)/1024)
print"剩余内存:%sM" % (int(MemFree)/1024) 原文:https://www.cnblogs.com/pzk7788/p/10255567.html
1.简单输入输出交互。>>> name=input(‘please input:‘)
please input:22
>>> print(name)
22
>>> n=input(‘:‘)
:23
>>> name+n
‘2223‘
>>> int(name)+int(n)
45
>>> name=‘Zhouy‘
>>> print(‘Hi,%s.‘%name)
Hi,Zhouy.
>>> 2.用户输入两个数字,计算并输出两个数字之和:(尝试只用一行代码实现这个功能)#用户输入数字
a=input(‘请输入第一个数字:‘)
b=input(‘请输入第二个数字:‘)#求和
sum=float(a)+float(b)#显示...
本题要求编写程序,统计并输出某给定字符在给定字符串中出现的次数。输入格式:输入第一行给出一个以回车结束的字符串(少于80个字符);第二行输入一个字符。输出格式:在一行中输出给定字符在给定字符串中出现的次数。代码如下:#!/usr/bin/python# -*- coding: utf-8 -*-n = input()
s = str(input())key = 0
for i in range(0,len(n)):if s == n[i]:key +=1
print(key)这个程序简单,遍历一下输入的字符串就行。刚开始我想的是...
写一个程序,用于计算2016年9月23日是这一年的第几天?def isLeap(year):#year=input("please enter year:")try:if year%4==0 and year%100!=0:return Trueelif year%400==0:return Trueelse:return Falseexcept:return‘your enter is not year!‘print isLeap(2016)def daysss(year,month,date):m30=[4,6,7,9,11]m31=[1,3,5,8,10,12]days=0for i in range(1,month):if i in m30:days=days+30elif i in m31:days=days+31elif i==...
题目:填充每个节点的下一个右侧节点指针:给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:struct Node { int val; Node *left; Node *right; Node *next;}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都...
【Python练习题 028】 求一个3*3矩阵对角线元素之和-----------------------------------------------------这题解倒是解出来了,但总觉得代码太啰嗦。矩阵这东西,应该有个很现成的方法可以直接计算才对…… 啰嗦代码如下:str = input(‘请输入9个数字,用空格隔开,以形成3*3矩阵:‘)
n = [int(i) for i in str.split(‘ ‘)] #获取9个数字
mx = [] #存储矩阵
for i in range(3):mt = [] #收集矩阵中每行的数字列表for j in...
‘‘‘利用字典实现登录‘‘‘users=[{‘username‘:‘jerry‘,‘pwd‘:‘123456‘},{‘username‘:‘tom‘,‘pwd‘:‘1‘}]def login(username,pwd): print(username,pwd) if(username==‘‘or pwd==‘‘): return 0 else: isbool=True for dict1 in users: if (dict1[‘username‘]==username and dict1[‘pwd‘]==pwd): return True else: ...
1bicycles = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]print(bicycles)print(bicycles[0]) ##第一个列表数据print(bicycles[0].title())print(bicycles[-1].title()) ##从最后开始数messge = "my first bicycle was a "+bicycles[2].title()+"."print (messge)
2name = [‘Laoli‘,‘zaoxu‘,‘Caocao‘,‘libai‘,‘dufu‘,‘dazhangwei‘]print(name[0],name[1],name[2],name[3])3motorcycles = [‘honda‘, ‘...
#!python3
#运行 py mcb.pyw save spam ,剪贴板内容就关键字spam保存
#py mcb.pyw spam 加载spam 对应内容到剪贴板
#py mcb.pyw list 关键字列表到剪贴板#扩展多重剪贴板:
#增加delete <keyword>,从shelf中删除相应键值
#delete all 清空
import sys,pyperclip,shelvemcbShelf=shelve.open(r‘I:\mcb‘)#save clipboasr content:
if len(sys.argv) == 3:if sys.argv[1].lower()==‘save‘ and sys.argv[2].lower() != ‘all‘:m...
具体请看链接:链接 2018-09-2912:51:45原文:https://www.cnblogs.com/ChaoyuanJam/p/9723150.html
Django第一题s = "Alex SB 哈哈\r\nx:1\r\ny:2\r\nz:3\r\n\r\n自行车"1. 如何取到["Alex SB 哈哈\r\nx:1\r\ny:2\r\nz:3", "自行车"]?ret = s.split('\r\n\r\n')
print(ret)2. 如何在上面结果基础上拿到["Alex", "SB", "哈哈"]?ret = s.strip().split('\r\n')
res = ret[0] # Alex SB 哈哈
print(res.split(' ')) # ['Alex', 'SB', '哈哈']3. 如何在上面结果基础上拿到"SB"?print(res.split(' ')[1])第二题有一个列表,他的内部是...
Python中的变量名->字母 ‘name‘,‘age‘, ->数字 ‘name1‘,‘age1‘->下划线 ‘name_1‘,‘age_1‘注意:不能以数字开头 例:1name_,不能是Python中的关键字,最好不要与Python内置的东西重复,如:‘and‘,‘as‘,‘break‘,‘class‘....补充:在写变量名时,尽量让变量名起的有意义一些,如用户ID应该写作user_id较为合适。 数据类型字符串类型在Python中引号之中的内容即字符串。如:name1 = ‘萨摩耶‘
na...
回顾里下上节课讲的课程,主要有格式化和编码内容。(1)格式化输出使用%s和%d(占位符),如果想输出%,则使用%%。(2)编码:ascii码只能显示英文,特殊符号,数字。万国码 unicod 最开始是16位,由于中文字节太多,使用32位(4个字节),但是四个字节又太多,占用资源多。于是升级utf-8,其最少用一个字节,8位表示一个英文字母,欧洲有些国家采用16位,两个字节,亚洲中国使用24位,三个字节。gbk码是中国在ascii码的基础上做的...
本题来自 Project Euler 第9题:https://projecteuler.net/problem=9# Project Euler: Problem 9: Special Pythagorean triplet
# A Pythagorean triplet is a set of three natural numbers,
# a < b < c, for which, a**2 + b**2 = c**2
# For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2.
# There exists exactly one Pythagorean triplet for which a + b + c = 1000.
# Find the product abc.
# Answer: 31875000for a in ran...