Python2 study notes (2)

Python basic syntax

  • Python syntax uses indentation, generally using the tab key or 4 spaces. Under normal circumstances, do not mix these two indentation formats, you may get an error
  • When the statement ends with ":", the indented statement is treated as a code block
  • The pyhon program is case sensitive

type of data

The data types that can be directly processed in Python include integers, floating point numbers, strings, Boolean values, and null values. In addition, python also provides data types such as list and dictionary. It also allows custom data types.

  • Integer and floating-point number: The storage mechanism of integer and floating-point number in computer is different. Like other languages, in integer division in python, the remainder is still an integer.
>>> 30/3
10
>>> 10/3
3
>>> 10.0/3
3.3333333333333335
>>> 10/3.0
3.3333333333333335
  • String: The content enclosed in single or double quotation marks, but the content of the string does not include the quotation marks themselves. If there are quotation marks in the string, use transfer characters or use another kind of quotation mark as a string mark.
>>> print 'abc'
abc
>>> print "ABC"
ABC
>>> print "I'am a student"
I'am a student
>>> print 'I\'m a student'#使用转移字符输入字符串中需要的单引号
I'm a student
>>> print r"I\'m a student"#使用r""使转义字符不转义
I\'m a student
  • Boolean value: There are two values, True and False. In python, Boolean values ​​can be represented by True and False, or calculated by Boolean calculations. Boolean values ​​can be calculated with and, or, and not. The rules of operation are the same as those in mathematics. It is also often used in condition judgment.
>>> True #直接使用
True
>>> False
False
>>> 3 > 2  #表达式计算
True
>>> True and False  #使用与或非计算
False
>>> True or False
True
>>> not True
False
>>> age = 18
>>> if age >= 18:  #运用于条件判断之中
...     print 'adult'
... else:
...     print'teenager'
... 
adult
  • Null value: a special value, represented by None. It cannot be understood as 0, because 0 is meaningful, and None is a special null value.

variable

Python is a dynamic language, it does not need to determine its data type when defining variables. And in python, a variable is actually a string object, which forms a {name:object} association with the value.

  • Variable name: Variables exist in the form of variable names in the program, and their names are a combination of letters, numbers and _, and cannot start with a number
>>> a = 3
>>> a3 = 'abc'
>>> a_4 = '123'
>>> _123a = 567
>>> 12as = 34  #以数字开头会报错
  File "<stdin>", line 1
    12as = 34
       ^
SyntaxError: invalid syntax
  • In python, the same variable can be repeatedly assigned different types of values
>>> a = 123
>>> a = 'iop'
>>> a = True
  • In python, you can assign a variable to another variable.
>>> a = True
>>> b = a
>>> a = 123
>>> b
True
>>> a
123

This operation actually points variable b to the data pointed to by variable a instead of pointing to variable a, so the value of variable a changes, and the value of variable b does not change.

constant

Constants are variables that cannot be changed. They are generally represented by certain capital letters, such as PI for the pi. But python does not have any mechanism to guarantee that PI will not be changed.


Character Encoding

Computers can only process numbers. To process text, you need to convert them into numbers. There are ASICC encoding, Unicode encoding and UTF-8 encoding in python

  • ASICC code: 1 byte, including English letters, numbers and some characters;
  • Unicode encoding: generally 2 bytes represent a character, which is supported by most operating systems and programming languages;
  • UTF-8: is a variable-length encoding. UTF-8 encoding is encoded into 1-6 bytes according to different number sizes. Commonly used English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes. Only rare characters will be encoded into 4- 6 bytes.

ASICC encoding is part of UTF-8 encoding

Nowadays, the usual encoding method of computer working system is: in memory, use Unicode encoding; when it needs to save to hard disk or transfer, use UTF-8 encoding.

python string

  • Can exchange letters with corresponding characters
>>> ord('a')
97
>>> chr(97)
'a'
  • Characters u'...'represented in Unicode are represented by
>>> print u'中文'
中文
>>> print u'123'
123
  • Function to convert Unicode encoding to UTF-8 encodingencode('UTF-8')
>>> u'ABC'.encode('UTF-8')
'ABC'
>>> u'中文'.encode('utf-8') #一个字符转换成3个字符
'\xe4\xb8\xad\xe6\x96\x87'
  • Function to convert UTF-8 encoding to Unicode encodingdecode('utf-8')
>>> '123'.decode('utf-8')
u'123'
>>> '\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
u'\u4e2d\u6587'
>>> print '\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
中文

Note: When saving the source code, you must specify the save as UTF-8 encoding format, and add in the first line of the source code# -*- coding : utf-8 -*-

#!/usr/bin/env python  
# -*- coding: utf-8 -*-
name = raw_input('请输入一个名字:')
print 'hello,',name
----------
[zhang@localhost python]$ ./hello.py 
请输入一个名字:张三
hello, 张三

If you use Notepad++ for editing, in addition to adding #-- coding: utf-8-- , Chinese strings must be Unicode strings.

String formatting

In daily life, I often encounter semi-filled dialogues such as'Mr. xx, find you x yuanqian'. Here we need to use the formatted input of strings

In Python, the% operator is used for formatting characters. Commonly used placeholders are:

  • %d integer
  • %f floating point
  • %s string
>>> 'hello,%s '% 'world'
'hello,world '
>>> print u'找您%d元钱' % 10
找您10元钱
>>> '%.2f' % 3.14
'3.14'
  • Add a number between the% and the placeholder to specify the number of integers and decimals
>>> '%3d' % 7  #三位数右对齐
'  7'
>>> '%03d' % 7  #以0补全缺失位
'007'
>>> '%.5f' % 3.14
'3.14000'
  • %s can convert any data type into a string
>>> '%s' % 25
'25'
>>> '%s' % 3.14
'3.14'
>>> '%s' % True
'True'

Guess you like

Origin blog.csdn.net/jjt_zaj/article/details/51497168