Python是动态类型语言 ,也是若类型语言这种 语言特性就决定了 他不会有多么的复杂。。
#简单的输出打印
#coding=utf-8
import time; # This is required to include time module.
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
print paragraph
a,b,c=1,2,"aaa"
#List类似数组
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
if word=="word":
print paragraph[1:10]*2
list[1]="aaa"
print list[1:2]
#tuple类似 list 不可以赋值 只能read
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple + tinytuple
#元数组操作
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print tinydict
print dict
print chr(100)
#运算符
print 5**100
if not (1>1 and 2<2):
print "aaaa"
else:
print "hjjjjjjj"
a = 20
b = 20
if ( a is b ):
print "Line 1 - a and b have same identity"
else:
print "Line 1 - a and b do not have same identity"
#for 循环
for letter in "abcdefghi":
print letter
#遍历List
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print 'Current fruit :', fruits[index]
#遍历trupl 通过索引循环
tp = ('banana', 'apple', 'mango')
for index in range(len(tp)):
print 'Current fruit :', tp[index]
#遍历元字段
dict={"a":1,"b":2}
for index in dict:
print index,':', dict[index]
#字典长度
print len(dict)
#列出banana元素出现的次数
print fruits.count('banana')
#time
ticks = time.time()
print ticks
#定义函数
def printList(list):
for i in range(len(list)):
print list[i]
print locals()
printList(fruits)
#异常处理
try:
print 2/0
except Exception:
print "ssssssss"
else:
print "no exception"
#try finally
#即使出现异常finally还是能执行的
#raise触发异常
try:
print 1/0
finally:
print "finally"
#元组进行格式化
#coding=utf-8
class Student:
name=""
age =0
def showInfo(self):
print "Name:%s,Age:%s" % (self.name,self.age)
def __init__(self,name,age):
self.name=name
self.age=age
student=Student("aaa",11)
student.showInfo()
#获取类的属性
#coding=utf-8
#!/usr/bin/python
class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__
#动态类型操作
#codeing=utf-8
class Data:
name =""
def __init__(self,name):
self.name=name
print "__init__"
def __del__(self):
print "__del__"
def __str__( self ):
return self.name
data=Data("tom")
def F():
print "aaaaaaa"
data.fun=F
data.fun()
data.age=100
print data.age
#自定义__str__输出
print data
python基础练习
最新推荐文章于 2024-05-09 06:49:29 发布