python基础(变量,续行符,is,round,if,字符串,日期,数学,参数)
2016-03-05 11:37
537 查看
#coding=utf-8 #print函数 print 3, -1, 3.14159, -2.8 #type函数 print type(3), type(3.14159), type("123") #类型转换 print int(3.14159), int(-2.8) print float(3), float(-1) #输出字符串 print "span" + "and" + "eggs" str1 = "teacher" str2 = "student" print "I'm a %s, not a %s"%(str1, str2) s = "123" print type(s) == str #次方 print 3 ** 3 print 81 ** 0.5
[b]一.变量[/b]
#coding=utf-8 #变量 meal = 40 tip = 0.5 total = meal * (1 + tip) print "%.2f"%total fifth_letter = "MONTY"[4] print fifth_letter #正常除,地板除 print 1 / 2.0 print 1 // 2.0 #复数 x = 2.4 + 5.6j print x.imag print x.real print x.conjugate()
[b]二.续行符[/b]
#coding=utf-8 #续行符 a = "aaaaaa"\ "bbbbbbb" print a print "---------------" #(),[],{},'''可以不用续行 a = ("aaaaa" "bbbbb") print a print "---------------" a = ["aaaaa" "bbbbbbb"] print a print "---------------" a = {"aaaaaa" "bbbbbbb"} print a print "---------------" a = '''aaaaaaaaa bbbbbbbbb''' print a print "---------------"
[b]三.is,round函数[/b]
#coding=utf-8 #is是通过对象id判断 a = 100.0 b = 100 c = 100 print a == b print a is b print c is b print "------------" #四舍五入 print round(3.4) print round(3.5) print "------------" #函数 def spam(): eggs = 12 return eggs print spam()
四.if结构,函数
if True: pass elif True: pass else: pass #continue,break print 8 > 4 > 2 print 8 > 4 == 4 print "-----------" def f(x, y): pass print f(68, False) print f(y = False, x = 68) #print f(y = False, 68),error
[b]五.局部全局变量[/b]
#coding=utf-8 """ 全局,局部变量 """ num = 4 def f(): num = 3 f() print num #4 def g(): global num num = 3 g() print num #3
[b] 六.字符串[/b]
#coding=utf-8 #字符串 a = "Xsxx" print len(a) print a.lower() print a.upper() print a.isalpha() print a.istitle()#首字母大写,其他字母小写s print a[0] print a[1:3] print type(str(3.14))
[b]七.键盘输入[/b]
#coding=utf-8 #从键盘输入 num = raw_input("what's the number?")
[b]八.日期[/b]
#coding=utf-8 #日期 from datetime import datetime now = datetime.now() print now print now.year print now.month print now.day print "%s:%s:%s"%(now.hour, now.minute, now.second)
[b]九.数学[/b]
#coding=utf-8 #数学 from math import * print sqrt(25) a = [1,2,3,4,5] print max(a) print min(a) print abs(-5) #print dir(math),error:no math import math print dir(math)
[b]十.参数[/b]
#coding=utf-8 #参数不确定时,*args,**kwargs(**kwargs有key值) a = [1, 2, 3, 4] b = 4 c = '111' def f(*args): for i in args: print i f(a,b,c) def g(**kwargs): print kwargs g(z = 1, k = 2, l = 3)
相关文章推荐
- python-Day6
- 基于python快速实现排列组合算法
- Python 基础语法(四)
- Python 基础语法(三)
- Python 基础语法(二)
- Python 基础语法(一)
- python中的cm()len() list() and tuple()以及一些内建函数
- python 学习 4
- Python文件夹与文件的操作
- numpy.ndarray.flatten
- python 3.3 下载固定链接文件并保存
- ubuntu上跑python连接pg,报错 ImportError: No module named psycopg2
- python获取父类的子类(遍历,递归),并循环执行所有子类的某一方法
- Python核心教程(第二章)
- python学习3
- 安装pip出现的问题及解决办法
- python list and tuple
- Python之模拟职场人生游戏
- 用户文章评论
- Python图像处理库PIL的ImagePath模块介绍