Python学习

一、输出
print "hello world"
退出Python解释器
quit(),exit(),ctrl+D
二、新建Python文件
[badou@master python_test]$ touch 1.py
#!/usr/bin/python 指定目录,告诉操作系统执行这个脚本时,调用/usr/bin下的Python解释器
#!/usr/bin/env python 防止操作系统用户没有将Python装在默认路径,会先到env设置里查找Python的安装路径,再调用对应路径下的解释器
[root@master python_test]# vim 1.py    
#!/usr/bin/python
print "hello world"
[root@master python_test]# python 1.py 
hello world

三Python语法
1、输出字符串
print "hello world"
name='zhangsan'<!--定义变量-->
print "your name is %s" %(name)
print "your name is %s" %("zhangsan")
print "your name is" , name

2、整数
print "he is %d years old" %(14)
3、浮点数
print "his height is %f m" %(1.82)
4、指定小数位的浮点数
print "his height is %.2f m" %(1.82)
5、指定占位符右对齐
print "name:%10s Age:%8d height:%8.2f" %("lisi",25,1.83)
6、指定占位符左对齐
print "name:%-10s age:%-8d height:%-8.2f" %("lisi",25,1.83)
7、科学计数法
format(0.0015,'.2e')     
四、函数
因为函数有返回值,如果不加return的话,会出现none
def print_your_name(name):
        print "his name is ", name
print print_your_name("1")
print print_your_name("2")
输出
his name is  1
None
his name is  2
None

def print_your_name(name):
        print "his name is ", name
        return ("%s,is a good man" %name)
print print_your_name("1")
print print_your_name("2")
输出
his name is  1
1,is a good man
his name is  2
2,is a good man
hello world                     

五、数据结构
hashmap,array,set
python 中数据结构会变化
hashmap ->dict->{}
array ->list->[]
set ->set->set()
用法
#dict
color={"red":0.2,"green":0.2}
print color
print color["red"]

#list
color_list=["red","blue"]
print color_list
print color_list[1]

#set同Javaset
a_set = set()
a_set.add("111")
a_set.add("222")
a_set.add("111")
print a_set

六、条件
#if

a=1
if a>0:
  print 'a gt 0'
elif a==0:
 print 'a eq 0'
else:
 print 'a lt 0'

#for
a_list =[]
a_list.append('11')
a_list.append('33')
a_list.append('22')

for value in a_list:
   print value

b_dict={}
b_dict['aa']=1
b_dict['bb']=2
b_dict['cc']=3
for value in b_dict:
  print value

for key,value in b_dict.items():
  print key+" "+str(value)

c_set =set()
c_set.add('a')
c_set.add('b')
c_set.add('c')
for value in c_set:
  print value

sum=0
for value in range(1,11):
  sum+=value
  print value
  print sum

#while
cnt=2
while cnt>0:
  print 'aaa'
  cnt -=1

#print dual
i=1
while i<10:
  i+=1
  if i%2>0:
    continue
  print i

七、字符串
#String
str ='ABCDEF'
print len(str)
print str[2:5]

print str.lower()

八、异常

#try...catch

try:
  a=6
  b=a/0
except Exception,e:
  print Exception,":",e

try:
  print '1111'
  fh=open('testfile','r')
  print '2222'
except IOError,e:
  print '3333:',e
else:
  print '4444'
  fh.close()
九、导包

#import module

import math
print math.pow(2,3)
print round(4.9)

import random
items =[1,2,3,4,5,6]
random.shuffle(items)
print items

a=random.randint(0,3)
print a
//随机取三个
s_list = random.sample('abcdefg',3)
print s_list

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值