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]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- Android MediaPlayer的生命周期
MediaPlayer的状态转换图也表征了它的生命周期,如下: 这张状态转换图清晰的描述了MediaPlayer的各个状态,也列举了主要的方法的调用时序,每种方法只能在一些特定的状态下使用,如果使用时 ...
- 【Android】HorizontalScrollView内子控件横向拖拽
前言 网上ListView上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园 ...
- 用最简单话概括SSH三框架
Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句. Struts是用来做应用层的,他它负责调用业务逻辑serivce层,所以S ...
- laravel excel迁移到lumen
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出 ...
- maven-shade-plugin
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- java中equals和"=="的区别
"=="号,它比较的是一个对象在内存中的地址值, 比如2个字符串对象String s1 = new String("str");String s2 = new ...
- 初识Hadoop入门介绍
初识hadoop入门介绍 Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. < ...
- Android中在sdcard上创建文件夹
//在SD卡上创建一个文件夹 public void createSDCardDir(){ if(Environment.MEDIA_MOUNTED.equals(Environment ...
- Linux 下子线程的 pthread_cleanup_push() 和 pthread_cleanup_pop() 研究
线程退出前可能有一些清理工作,但是这部分代码又不会放到线程主体部分,就需要挂接一个或者几个线程“清洁工”来做这部分事情.需要这对兄弟: #include<pthread.h> void p ...
- 《InsideUE4》-3-GamePlay架构(二)Level和World
UE4深入学习QQ群: 456247757 引言 上文谈到Actor和Component的关系,UE利用Actor的概念组成一片游戏对象森林,并利用Component组装扩展Actor的能力,让世界里 ...