python基础语法快速复习
作者:互联网
◆版权声明:本文出自胖喵~的博客,转载必须注明出处。
转载请注明出处:https://www.cnblogs.com/by-dream/p/12895967.html
基本判断
# -*- coding: utf-8 -*- # 变量初始化 i = 0 s = "abcde" j = [] k = {} # 基本判断 if i == 0 and j is not None: print 'test1' elif k is None: print 'test2' else: print 'test3'
输出
test1
循环
# 循环 s = "abcde" for word in s: print word for i in range(0, len(s)-1): print i, s[i]
输出
a b c d e 0 a 1 b 2 c 3 d
三目运算符
# 三目表达式 k = 1 m = 2 print 'k' if k == 1 else 'm' print 'k' if m == 1 else 'm'
输出
k m
字符串处理
# 字符串处理 s="ABCdefaa" print s.lower() print s.upper() print s[1:] print s.strip('a') print s.replace('a','z')
输出
abcdefaa ABCDEFAA BCdefaa ABCdef ABCdefzz
字符、数字判断
# 字符、数字判断 class Test: def function(self, n): print n, n.isdigit(), n.isalpha(), n.isalnum() t = Test() t.function("123abc") t.function("123456") t.function("abcdef")
输出
123abc False False True 123456 True False True abcdef False True True
数字处理
# -*- coding: utf-8 -*- import math # 数字处理 print '1<<2', 1<<2 print '1<<3', 1<<3 print '8>>2', 8>>2 # 平方 print pow(2,3) # 开方 print math.sqrt(36) print math.sqrt(25)
输出
1<<2 4 1<<3 8 8>>2 2 8 6.0 5.0
列表
# list 当栈用 l = [] l.append('a') l.append('b') l.append('c') l.append('a') print l, len(l), l[0], l[len(l)-1] print l.pop() print l
输出
['a', 'b', 'c', 'a'] 4 a a a ['a', 'b', 'c']
二维数组
# 二维数组 z = [[0 for column in range(3)] for row in range(2)] print z z[1][1] = 1 print z
输出
[[0, 0, 0], [0, 0, 0]] [[0, 0, 0], [0, 1, 0]]
Map
# map m = {} m['bob'] = 18 m['jack'] = 20 print m, m.has_key('jack'), m.has_key('liming')
输出
{'bob': 18, 'jack': 20} True False
堆
# -*- coding: utf-8 -*- from heapq import * # 堆 # (小顶堆) 每次pop出最小的值 hp = [ 9, 1, 8 ] # 堆化 heapify(hp) print hp print heappop(hp) print hp heappush(hp, 11) heappop(hp) print hp
输出
[1, 9, 8] 1 [8, 9] [9, 11]
标签:输出,False,复习,python,hp,语法,print,True,append 来源: https://www.cnblogs.com/by-dream/p/16286274.html