Python(0):Python基础知识
来源:互联网 发布:域名不合法是怎么回事 编辑:程序博客网 时间:2024/06/06 21:44
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#字符串--Stringstr = 'hello world'print strprint str[0]print str[2:5]print str[2:]print str*2print 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*2print list+tinylist#3#元组--tuple--(逗号隔开)--不能二次赋值tuple = ('runoob',789,2.23,'john',23.3)tinytuple = (689,'john')print tupleprint tuple[0]print tuple[1:3]print tuple[2:]print tinytuple*3print 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 tinydictprint tinydict.keys()print tinydict.values()''''''#***3*****运算符***********#a = 21b = 10c = 0if(a == b): print "1- a=b"else: print "1- a!=b"''''''#***4****运算符“in”“not in”************#a = 10b = 20list = [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 = 20b = 20if(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 numbersprint evenprint odd''''''#***6********猜大小游戏**************#s = int(random.uniform(1,10))print sm = 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:',letterfruits = ['banana','apple','mango']for fruit in fruits: print 'now:',fruitfor 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 = 2while(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:',tickslocaltime = time.localtime(ticks)print 'localtime:',localtimeprint '%dyear:%dmonth:%dday:%dhour:%dminute:%dsecond'%(localtime[0], localtime[1],localtime[2],localtime[3],localtime[4],localtime[5])localtime = time.asctime(localtime)print 'now:',localtimeprint 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 = 2000def AddMoney(): global money money = money + 1print moneyAddMoney()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.closedprint 'access mode:',fo.mode print 'add space force',fo.softspacefo.write("12kjhkl3\r\n");fo.write("\n4534\nojoj\n");#print fo.read(10)fo.close'''
阅读全文
0 0
- Python(0):Python基础知识
- python黑客[0]-基础知识
- Python基础知识(一)
- Python基础知识(一)
- python学习(基础知识)
- python基础知识(一)
- Python (一)基础知识
- python基础知识(2)
- python基础知识(3)
- Python基础知识(一)
- Python基础知识(二)
- Python 基础知识(1)
- Python基础知识(一)
- python基础知识(一)
- python基础知识
- Python 基础知识
- Python 基础知识
- python基础知识
- [codeforces#438 E题]Policeman and a Tree
- Struts2框架详细解析
- bzoj 1225: [HNOI2001] 求正整数
- Wpf Windows SizeToContent
- 避免 php-fpm 耗尽内存导致宕机
- Python(0):Python基础知识
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
- github
- Git学习简单教程
- sql语句的增删改查
- 【并行计算-CUDA开发】FPGA 设计者应该学习 OpenCL及爱上OpenCL的十个理由
- Spring MVC之@RequestMapping 详解
- Java——设置线程等待与线程唤醒
- 学会开发自己的注解