您的位置:首页 > 编程语言 > Python开发

Python(0):Python基础知识

2017-10-10 14:48 323 查看
1:输入输出;

2:Python变量类型(字符串、列表、元组、字典);

3:运算符;

4:运算符“in”“not in”;

5:身份运算符:“is”“is not”;

6:while循环、猜大小游戏(基于while);

7:for循环;

8:时间time、日历calendar;

9:自定义一个函数;

10:命名空间和作用域;

11:文本读写

#coding=utf-8 #编码(以支持中文)
import random #随机
import time #时间
import calendar #日历

'''
#***1******输出***********#
print"你好";

#***1******输入***********#
print 'input:'
str = input()
print str
'''

'''
#***2****Python变量类型***********#

#1#字符串--String
str = 'hello world'

print str
print str[0]
print str[2:5]
print str[2:]
print str*2
print str+"test"

#2#列表--List(有序的对象集合)--[]--类似数组
list = ['runoob',123,2.23,'john',79.2]
tinylist = [345,'john']

print list
print list[0]
print list[1:3]
print list[2:]
print tinylist*2
print list+tinylist

#3#元组--tuple--(逗号隔开)--不能二次赋值
tuple = ('runoob',789,2.23,'john',23.3)
tinytuple = (689,'john')

print tuple
print tuple[0]
print tuple[1:3]
print tuple[2:]
print tinytuple*3
print tuple+tinytuple

#4#字典--dictionary(无序的对象组合)--{'a':'b'}--键值对
dict = {}
dict['one'] = "this is one"
dict[2] = "this is two"

tinydict = {'name': 'john','code':6785,'dept':'scales'}

print dict['one']
print dict[2]
print tinydict
print tinydict.keys()
print tinydict.values()
'''

'''
#***3*****运算符***********#
a = 21
b = 10
c = 0

if(a == b):
print "1- a=b"
else:
print "1- a!=b"
'''

'''
#***4****运算符“in”“not in”************#
a = 10
b = 20
list = [1,2,3,4,5];

if(a in list):
print "*--a is in list"
else:
print "*--a is not in list"
'''

'''
#***5****身份运算符:“is”“is not”************#
a = 20
b = 20
if(a is b):
print "a is b"
else:
print "a is not b"
'''

'''
#***6***while循环**************#
numbers = [12,37,5,42,8,3]
even = []
odd = []
while len(numbers)>0:
number = numbers.pop()
if(number % 2 == 0):
even.append(number)
#continue
else:
odd.append(number)
#break
#else使用条件必须是while不成立了,break跳出时若依旧成立则else不执行
else:
print "while end"

print numbers
print even
print odd
'''

'''
#***6********猜大小游戏**************#
s = int(random.uniform(1,10))
print s
m = int(raw_input('input integer:'))
while m != s:
if m > s :
print('>')
m = int(input('input integer'))
if m < s :
print('<')
m = int(input('input integer'))
if m == s:
print('=')
m = int(input('input integer'))
break;
'''

'''
#***7***for循环*********#
for letter in 'python':
print 'now:',letter

fruits = ['banana','apple','mango']
for fruit in fruits:
print 'now:',fruit

for index in range(len(fruits)):
print 'now:',fruits[index]

for num in range(10,20):
for i in range(2,num):
if num%i == 0:
j = num / i
print '%d = %d * %d' % (num,i,j)
break
else:
print num,'is prime'
'''

'''
i = 2
while(i<100):
j = 2
while(j <= (i/j)):
if not(i%j):
break
j = j+1
if j>(i/j) :
print i,'is a prime number'
i = i+1
'''

'''
#***8******时间***********#
ticks = time.time() #当前时间戳
print 'now:',ticks
localtime = time.localtime(ticks)
print 'localtime:',localtime
print '%dyear:%dmonth:%dday:%dhour:%dminute:%dsecond'%(localtime[0],
localtime[1],localtime[2],localtime[3],localtime[4],localtime[5])
localtime = time.asctime(localtime)
print 'now:',localtime

print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

cal = calendar.month(2017,8)
print cal
'''

'''
#***9******自定义一个函数***********#
def printme(str):
"print str" #此函数的文档说明
print str #执行语句
return

printme("hello") #调用自定义的函数
'''

'''
#***10******命名空间和作用域***********#
#Python 会智能地猜测一个变量是局部的还是全局的,
#它假设任何在函数内赋值的变量都是局部的。
#因此,如果要给函数内的全局变量赋值,必须使用 global 语句。
money = 2000
def AddMoney():
global money
money = money + 1

print money
AddMoney()
print money
'''

'''
#***11******文本读写***********#
#fo = open("foo.txt",'wb+')
fo = open("F:\\Python\\Project\\1test\\foo.txt","rb+")
print 'name:',fo.name
print 'is close?:',fo.closed
print 'access mode:',fo.mode
print 'add space force',fo.softspace
fo.write("12kjhkl3\r\n");
fo.write("\n4534\nojoj\n");
#print fo.read(10)
fo.close
'''
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: