# learn_python **Repository Path**: xuliangbo/learn_python ## Basic Information - **Project Name**: learn_python - **Description**: learn_python跟练 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 9 - **Forks**: 1 - **Created**: 2020-07-18 - **Last Updated**: 2023-07-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Python基础 > python3支持 `UTF-8` 字符集,python区分大小写。 > python2支持中文较差 需要再文件头 `#coding:utf-8` ### print函数 ```python #变量是真的变量,动态类型 name = 'King' #合法的 name = 787 #print a,b,c必须都是str print(a,b,c) #需求帮助的方法 #python3进入控制台后, >>>help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. #不用默认的空格‘ ’分割可 改成 ‘|’分割 print(a,b,c,sep = '|') print(a,b,c,sep='|',end = '') # \n \t \' 转移字符 >>> print('乔治说:\'我要吃饭\' ') 乔治说:'我要吃饭' #输入代码对比结果: # r 代表 内部字符串默认不转义 print('乔治说:“想吃饭”') print('\ahahah') print('hello\py\\thon') print('hello\py\thon') #结果 -bash-3.2$ python3 chapter001.py 乔治说:“想吃饭” hahah hello\py\thon hello\py hon #占位符用法 %d %f %.2f a='徐' b='亮' c='波' print('添加了%s,12%s1213%s' % (a,b,c)) 添加了徐,12亮1213波 # print('{}asdasd{}asda{}'.format(a,b,c)) print('添加了%s,12%s1213%s' % (a,b,c)) # ``` ### 比较运算 > 控制器中的 -5~~256 的值 会保存在同一个内存地址,大数值不会 ```python a = 10 b = 10 print(a is b) print(a == b) c = 2000000 d = 2000000 print(c is d) print(c == d) ## is 相当于 id(a) == id(b) print(id(c)) print(id(d)) # 幂运算符 print(2**8) print("*" * 30) print('\t\thahaha') print("*" * 30) # 输出 256 ****************************** hahaha ****************************** # 精确的 a = 9/4 # 地板除 b = 9//4 ``` ### and or not #### 三元运算符 ``` (a+b) if a > b else (a-b) ``` ##### 缩紧 对应 if条件执行语句 ``` # '' 0 None 默认是False username = 'king' if username: print('alskdjla') // 以️:结束时 缩进语句视为 代码块 if(3<4): xxx elif 5>6: sss else: pass ``` ### range函数 ``` range(0,50,5) class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. | These are exactly the valid indices for a list of 4 elements. | When step is given, it specifies the increment (or decrement). | | Methods defined here: | | __bool__(self, /) | self != 0 | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(self, key, /) | Return self[key]. | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self integer -- return number of occurrences of value | | index(...) | rangeobject.index(value) -> integer -- return index of value. | Raise ValueError if the value is not present. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type : ``` ``` for i in range(20): print('hello') ``` #### chmod ``` u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。 + 加号 表示增加权限、- 表示取消权限、= 表示唯一设定权限。 r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该文件是个子目录或者该文件已经被设定过为可执行。 ``` ``` chmod a+x hello.py ``` ## 字符 ``` ord('A') chr(66) chr(25991) 'ABC'.encode('ascii') ```