Python简单实例
来源:互联网 发布:bsb的网络意思是什么 编辑:程序博客网 时间:2024/05/23 17:51
1,基础语法
1)print自带换行,取消换行?
x=raw_input()y=raw_input()z=raw_input()lst=[]lst.append(x)lst.append(y)lst.append(z)lst.sort()for i in range(0,3): print lst[i],
2)变量交换
a=1b=2a,b=b,aprint a,b
3)string.zfill()
str.zfill(x),在str前面填充0,以达到x宽度;
string.zfill(x,y),在x前面填充0,以达到y宽度。
import stringfor i in range(6): print string.zfill(i,5)str="hello"print str.zfill(10)
output:
00000000010000200003000040000500000hello
2,输出九九乘法表
for i in range(1,10): for j in range(1,i+1): print i,'*',j,'=',i*j, print ''
3,输出1-100之间的素数
import mathfor i in range(1,101): if i==1: continue else: flag=1 for j in range(2,int(math.sqrt(i))+1): if i%j==0: flag=0 break if(flag==1): print i
4,打印水仙花数
for i in range(1,10000+1): tmp=i s=0 while(tmp!=0): s+=pow(tmp%10,3) tmp/=10 if(s==i): print i
5,分解质因数
import mathx=int(raw_input("raw_input"))i=2k=xwhile(i<=k): if(x%i==0): x/=i if(x==1): print i break else: print i,'*', else: i+=1
6,统计字符串中的字母,空格,数字及其他的个数
s=raw_input()alpha=0space=0digit=0other=0for c in s: if c.isalpha(): alpha+=1 elif c.isspace(): space+=1 elif c.isdigit(): digit+=1 else: other+=1print alpha,space,digit,other
7,素数快速打表(筛选法)
import mathlst=[1]*104for i in range(2,int(math.sqrt(100))+1): if lst[i]==1: for j in range(i*2,100,i): lst[j]=0for i in range(2,100): if lst[i]==1: print i,' '
8,排序
1)调用内嵌函数sort对list排序
a=[]for i in range(5): a.append(int(raw_input()))a.sort()for i in range(5): print a[i],' ',
2)冒泡排序
a=[]for i in range(5): a.append(int(raw_input()))tmp=0for i in range(5): for j in range(5-i-1): if (a[j]>a[j+1]): tmp=a[j] a[j]=a[j+1] a[j+1]=tmp for i in range(5): print a[i],' ',
9,list的使用
1)list的插入与遍历
a=[0,10,20,30,40,50]x=int(raw_input())for i in range(0,len(a)): if a[i]>x: a.insert(i,x) breakfor y in a: print y,
10,文件读写
#-*-coding:utf-8-*-f=open("test.txt",'w')f.write("hello world ")f.write("hello python ")f.close()f=open("test.txt",'r')content=f.readlines() #读取文件中的所有行,存储在列表中print type(content)for i in range(len(content)): print content[i],
output:
<type 'list'>hello world hello python
0 0
- Python简单爬虫实例
- Python简单实例
- 简单Python爬虫实例
- boost python简单实例
- python简单实例一
- Python简单多线程实例
- python 简单线程池实例
- Python: tkinter实例 简单介绍
- Python: Sqlite3简单封装实例
- Python+Selenium 简单的实例
- 几个python简单爬虫实例
- 简单Python决策树可视化实例
- python之多线程简单实例
- python实例32[简单的HttpServer]
- 一个简单的python程序实例(通讯录)
- python实例32[简单的HttpServer]
- Python 读写文件的两个简单实例
- 简单的python爬虫抓取图片实例
- 09/01 号Java基础语法部分===》ATM机的设计过程
- 浏览器的高级使用
- Setting up a new Typescript 1.9 and React project
- 【Eclipse】将引用了第三方jar包的Java项目打包成jar文件的两种方法
- hiho1232 Couple Trees
- Python简单实例
- Codeforces Round #361 (Div. 2) 题解 粗鲁地二分 组合数学提炼模型
- 【NOIP2016提高A组模拟9.2】积木
- win10+vs2015+opencv2.X出现LNK2019、2001
- linux命令大全
- Mobile JavaScript Apps: The Dawn of React Native
- java中的session和cookies区别与联系
- 909422229__jQuery选择器总结
- IntelliJ IDEA下SSM框架整合、创建Maven项目流程(Spring + SpringMVC + MyBatis)