python学习笔记

来源:互联网 发布:龙珠gt战斗力官方数据 编辑:程序博客网 时间:2024/06/05 17:43
Python 2有31个关键字:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return

def for lambda try


In Python 3, exec is no longer a keyword, but nonlocal is.
在Python 3中,exec不在是一个关键字,但nonlocal是。
输出:
print(1)
运算优先级:
1.()
2.**指数
3.*/
4.+-
乱七八糟:
1.   求余%
bool == != > < >= <=
and or not
\n新行


2.   向下取整,而不是四舍五入


3.   字符串+*:
+:前后相连
*N:字符输出N次 'Spam'*3


4.   注释# 单行
注释""" """多行


5.   得到类型:type('dfsd')
转换类型: int(3.999)float(32) str(3.1415)


6.   math函数:
import math

math.sqrt(5) #5的平方根


7.   验证类型:
if isinstance(n, int):

8.   概念

封装:将部分代码包装在函数里
泛化:为函数增加一个形参。因为使得函数更一般
组合:一个函数内部调用另一个函数

新函数(function类型):
第一个例子:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."


def repeat_lyrics():
print_lyrics()
print_lyrics()


repeat_lyrics()
第二个例子:
def print_twice(bruce):
    print(bruce)
    print(bruce)


def cat_twice(part1, part2):
    cat = part1 + part2
    print_twice(cat)


cat_twice('4546','152545')


返回值:
def ret_list(a,b):
    l=[a,b]
    return l


c = ret_list('hg','fb')
print(c)
导入模块对象(常量、函数):
from math import pi
print(pi)
from math import *
cos(pi)
for循环:
for i in range(4):
    print('Hello!')
if:
if x < y:
    print('x < y')
elif x > y:
    print('x > y') 
else:
    pass #什么也不做
while:
while n > 0:
    if n == 1:
        break
    print(n)
    n = n - 1
break:跳出循环
递归:
def countdown(n):
    if n <= 0:
        print('Blastoff!')
    else:
        print(n)
        countdown(n-1)


countdown(6)
键盘输入:
input = input()
print(input)
name = input('What...is your name?\n')
print(name)
string
1.   name = 'xieyy'
letter = name[0] #name取首字母
2.   len(name) #长度
3.   遍历:
index = 0
while index <len(name):
    letter = name[index]
    print(letter)
    index = index + 1
4.   切片:
print(name[0:2])
print(name[:3])
print(name[2:])
字符串不可变,需改变可使用切片
greeting = 'Hello, world!'
new_greeting = 'J' + greeting[1:]
print(new_greeting) # Jello, world!
5.   计数:
word = 'banana'
count = 0
for letter in word:
    if letter == 'a':
        count = count + 1
print(count)
6.   返回大写upper
new_word = word.upper() # BANANA
7.   找到字母位置find
index = word.find('a')
index = word.find('an')
index = word.find('nan',4) #从第4个位置开始搜索
index = word.find('nan',4,6) #从第4个位置到第6个位置中间搜索
8.   第一个字符串是否在第二个字符串中间:in
'a' in 'banana' #返回True
9.   字符串比较 < > =
大写字母在小写字母前
读取:
fin = open('./words.txt')
line = fin.readline #从文件中读取字符直到到达新行
word = line.strip() #去掉空格
for word in fin: #得到文件中所有word
   print word   
列表:
1.   长度  
['eq ','fdd']
[10,20,['eq','fdd']] #嵌套,长度为3
2.   可变
number = [12,45,125,12]
number[1] = 'ad'
print(number) # [12,'ad',125,12]
3.   in的运用
result = 'ad' in number
print(result)
4.   for循环遍历
for num in number:
    print(num)
for i in range(len(number)):   #range返回一个从0到n(列表长度)的索引的列表
    number[i] = number[i]*2 
5.   +*的运用
+串连
*N 重复N次
6.   切片
t = [1,2,3,4,5,6]
t[1:3] = [9,9]
print(t) # [1, 9, 9, 4, 5, 6]
7.   列表方法
t = [1,2,3,4,5,6]
t.append(7)  #列表后追加一个字符 [1, 2, 3, 4, 5, 6, 7]
print(t)
t2 = [0,1]
t.extend(t2) #列表后追加一个列表 [1, 2, 3, 4, 5, 6, 7, 0, 1]
print(t)
t.sort() #列表从小到大排列 [0, 1, 1, 2, 3, 4, 5, 6, 7]
print(t)
8.   累加器sum()、削减
result = sum(t) #累加t中所有元素







0 0
原创粉丝点击