因为我的树莓派只要开机风扇就会不停的转为了控制风扇的转速我需要写一些脚本来控制gpio接口。所以作为兴趣就学习了一下python语言,当然python的实际作用远不止此,先看看基础的再深入的了解下吧。MacOS自带的Python是2.7x,但是我使用的是3.x 。当然这两者是有差异的,即使你不懂python也可以从版本号上看出来这个差异。 The key differences between Python 2.7.x and Python 3.x with examples <-这篇博客详细说明了2.x与3.x的区别。因为本人是从3.x学习python的所以后面的博文都会以3.x作为案例。
print (str) # Prints complete string print (str[0]) # Prints first character of the string print (str[2:5]) # Prints characters starting from 3rd to 5th print (str[2:]) # Prints string starting from 3rd character print (str * 2) # Prints string two times print (str + "TEST") # Prints concatenated string
print (tuple) # Prints complete tuple print (tuple[0]) # Prints first element of the tuple print (tuple[1:3]) # Prints elements starting from 2nd till 3rd print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints tuple two times print (tuple + tinytuple) # Prints concatenated tuple
print (dict['one']) # Prints value for 'one' key print (dict[2]) # Prints value for 2 key print (tinydict) # Prints complete dictionary print (tinydict.keys()) # Prints all the keys print (tinydict.values()) # Prints all the values #This is one #This is two #{'name': 'john', 'code': 6734, 'dept': 'sales'} #dict_keys(['name', 'code', 'dept']) #dict_values(['john', 6734, 'sales'])
Data Type Conversion(类型转换)
Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type-names as a function. 需要类型转换的时候只需要用类型名称作为一个方法就可以转换
S.No.
Function & Description
1
int(x [,base]) Converts x to an integer. The base specifies the base if x is a string.
2
float(x) Converts x to a floating-point number.
3
complex(real [,imag])Creates a complex number.
4
str(x) Converts object x to a string representation.
5
repr(x)Converts object x to an expression string.
6
eval(str)Evaluates a string and returns an object.
7
tuple(s)Converts s to a tuple.
8
list(s)Converts s to a list.
9
set(s)Converts s to a set.
10
dict(d)Creates a dictionary. d must be a sequence of (key,value) tuples.
11
frozenset(s)Converts s to a frozen set.
12
chr(x)Converts an integer to a character.
13
unichr(x)Converts an integer to a Unicode character.
14
ord(x)Converts a single character to its integer value.
15
hex(x)Converts an integer to a hexadecimal string.
16
oct(x)Converts an integer to an octal string.
流程控制
Decision Making && Loop
1 2 3 4 5 6 7 8 9 10 11 12 13
a = ["a","b","c",""]
for item in a: if item == "" : print("The Item is Empty") else: print(item) print("over") #a #b #c #The Item is Empty #over
Iterator and Generator
这块是伪代码,核心是 iter() 与 next()
1 2 3 4 5 6 7 8 9 10 11 12 13
list = [1,2,3,4] it = iter(list) # this builds an iterator object print (next(it)) #prints next available element in iterator Iterator object can be traversed using regular for statement !usr/bin/python3 for x in it: print (x, end=" ") or using next() function whileTrue: try: print (next(it)) except StopIteration: sys.exit() #you have to import sys module for this
Function
使用可变参数 *args 使用星号修饰
1 2 3 4 5 6 7
deffunctionA(content,*args): print(content) for var in args: print (var) return content