Meet Python: little notes
Source: http://www.liaoxuefeng.com/
❤ Escape character: '\'
- '\n': newline;
- '\t': tab;
- '\\': \;
- r'...': no transferring for contents within single quotes;
- '''...''': multiple lines within triple quotes: could start a new line with ENTER directly. r'''...''' is valid as well.
❤ Division
- '/': floating point calculation, alway return float;
- '//': only return integer part (decimal part is direclty abandoned);
- '%': return remainder.
❤ Character encoding
- Encoding standard
.. ASCII: some symbold, number, uppercase and lowercase, 1 byte representing 1 character;
.. GB2312: Chinese character;
.. Unicode: encoding characters in all languages using one criterion, 2 bytes representing 1 character usually;
.. UTF-8: to save space, converting Unicode into 1-6 bytes (usually, one English character: 1 bytes, while one Chinses: 3 bytes); criterion used in RAM;
- Python
.. encoding using unicode;
.. for sigle character, ord() - obtaining integral representing for the character; chr() - converting code into character;
.. b'...': converting string to byte format (one byte fot each character);
.. '...'.encode('method'): encode ... using corresponding method('ascii', 'utf-8', note that Chinese characters cannot be encoded using 'ascii');
.. b'...'.decode('method'): decode byte string '...' into characters using corredponding method ('ascii', 'utf-8');
.. len('...'): obtain number of characters or bytes in '...';
.. formatting characters: the same way as c (and matlab :p):
%s - character string;
%d: integer;
%f: floating;
%x: hexadecimal integer.
some examples:
>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000'.
>>> '%2d-%02d' % (3, 1)
'3-01'
>>> '%.2f' % 3.1415
'3.14'
>>> 'Age: %s; Gender: %s' % (25, True) # If you cannot decide which placeholder to use, just use %s
'Age: 25; Gender: true'
>>> 'Growth rate: %d %%' % 7
'Growth rate: 7 %'
NOTE: we should stick to 'utf-8' for converting to avoid chaos, to ensure which: 1. make sure your text editor is using: "UTF-8 without BOM" as encoding methods; 2. add the following two lines at the beginning of your script.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Meet Python: little notes的更多相关文章
- Meet Python: little notes 3 - function
Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...
- Meet Python: little notes 2
From this blog I will turn to Markdown for original writing. Source: http://www.liaoxuefeng.com/ ♥ l ...
- Meet python: little notes 4 - high-level characteristics
Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...
- python 100day notes(2)
python 100day notes(2) str str2 = 'abc123456' print(str1.endswith('!')) # True # 将字符串以指定的宽度居中并在两侧填充指 ...
- 70个注意的Python小Notes
Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要.旨在注意一些细节问题,在今后项目中灵活运用 ...
- [Python Study Notes]匿名函数
Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...
- [Python Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]with的使用
在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...
- [Python Study Notes]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- UIApplicationDelegate
App受到干扰时,UIApplication会通知它的delegate对象,让delegate处理系统事件. 项目中的AppDelegate已经遵守了UIApplicationDelegate协议 ...
- SVProgressHUD进度条蒙版
GitHub:https://github.com/samvermette/SVProgressHUDSVProgressHUD和MBProgressHUD效果差不多,不过不需要使用协议,同时也不需要 ...
- iOS-多线程--介绍NSOperration
一个NSOperation对象就代表一个操作,对象相当于GCD中的block. 一.NSOperation的作用: 配合使用NSOperation和NSOperationQueue也能实现多线程. 二 ...
- iOS 远程推送通知
1.什么是推送通知 在某些特殊情况下,应用程序被动收到的以不同种界面形式出现的提醒信息 推送通知的作用:可以让不在前台运行的app通知app发生了改变 iOS中得推送通知种类 远程推送通知(Remot ...
- vs出现“已经在解决方案中打开了具有该名称的项目”问题的解决方案
经过本人测试,这种问题一般出现在装了svn的项目. 其实删除了删除sln和csproj文件中的SVN配置信息就行了 需要删除的信息 sln文件中: GlobalSection(SubversionSc ...
- 体验最火的敏捷——SCRUM(厦门,2014.1.4)
1.概述SCRUM是当前最火的一种敏捷开发方法,有用户故事.冲刺.燃尽图等很多很酷的玩法,有牛B的产品负责人.SCRUM Master,有超强的自组织团队.本沙龙将为您展现当前最火最酷的敏捷开发方法! ...
- 传统模式下WebService与WebAPI的相同与不同
1.WebService是利用HTTP管道实现了RPC的一种规范形式,放弃了对HTTP原生特征与语义的完备支持:而WebAPI是要保留HTTP原生特征与语义的同时实现RPC,但WebAPI的实现风格可 ...
- 坑!坑!坑!防不胜防的unsigned int的运算
我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...
- python中mysqldb的用法
1.引入MySQLdb库 import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="ro ...
- Linux下NDK编译FFMPEG包含neon参数
FFMPEG编译成Android库已经有很多案例了,编译优化neon的也很多,以下是我通过实践成功的案例,这里主要讲编译的配置文件,其他设置可结合Linux下使用NDK编译FFMPEG(libstag ...