Basics of Python - identifiers, variables, data type (numeric / strings / sets / list / tuple / dictionary), the operator

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/99704833

The basic syntax

Identifier: used to identify names of variables, functions, classes, modules, and objects.
Python identifier may contain letters (AZ, az), numbers (0-9), and the underscore character (_), but has the following limitations:
1) the identifier of the first character must be a letter of the alphabet or underscore '_', and the variable name can not have spaces between
2) Python identifier is case-sensitive
3) in Python 3, the non-ASCII identifier is allowed
4) as a reserved word identifier may not .
Variables: you can name in the Python interpreter variables declared directly, without having to declare a variable of type, Python will automatically determine the type of the variable.

type of data:

1.Number (Digital)

Python 3 supports int (integer), float (floating point), bool (Boolean value), complex (complex) four kinds of digital type.

2.String (string)

Belonging to the sequence type String (sequence type).
Python does not support single character type, single character as a string in Python is using. Python access substrings, brackets may be used to intercept the string. For example the string "Parrot", Python is considered within the "P", "a", a combination of "r", "r", "o", "t" 6 characters. Like the index list, the index value of the first character is always 0, so the access string "Parrot" when the first character "P" "Parrot" [0 ].
If the index value is negative, it represents a forward end of the string.
You can also use a colon (:) dividing specified range of characters, for example: s [a: b] represents the index taken from the start of a character, the index b-1 until the characters.
After the string is not disposed directly modified, but the above means can be used to modify the segmentation and merging, essentially reconstruct the string by assignment.
a, string methods
. 1) capitalize () method to capitalize the first letter of the string, the other letters to lower case
2) Upper () method of the letter string is converted to uppercase
. 3) Lower () method of the string converted to lower case letters
. 4) title () method returns the "heading" of the character string, i.e., all words are capitalized started, the remaining letters in lower case
. 5) swapcase () method is used to convert lower case letters in the string

. 6) isalnum () method for detecting whether a string of letters and numbers. If there is at least a character string and all the characters are letters or numbers returns True, otherwise return False
. 7) the isalpha () method detects whether the string only the alphabet. If there is at least a character string and the characters are all the letters or characters returns True, otherwise return False
. 8) isdight () method detects whether the string only consist of digits. If the string only contains numbers returns True, otherwise False

9) COUNT () method is used in a number of statistical string of characters appears. Optional parameters at the beginning and end of the string search.
Syntax: str.count (sub, start = 0 , end = len (str)) which is a sub substring search; start position to start the search for the string, the first character of the default index value of zero. end position for the end of the search string, the default is the last position of the string.
10) Find () whether a detected string contains substring, if the index value comprises a start is returned, otherwise -1
syntax: str.find (sub, start = 0 , end = len (str)) wherein sub specified string; start is the starting index, the default value is 0; end to end index, the default is the last position of the string.
. 11) index () method for detecting whether a string contains the substring. If the index value comprising a substring starts is returned, otherwise an exception will be reported
syntax: str.index (sub, start = 0 , end = len (str)) wherein the specified search string sub; start is the start index the default is 0; end to end index, the default is the last position of the string.
12 is) the Join () method is used to sequence the elements are connected to the specified character string to generate a new
syntax: str.join (sequence) in which the sequence is the sequence of elements to be connected
13 is) Replace ()The method of replacing the old string to string new string.
Syntax: str.replace (old, new [, max]) where is the old sub-string will be replaced, new new for the new string, which replaces old substring, max is optional, represents not more than replacement max times
14) max () / min () method returns the maximum / minimum value string

b, string formatting
Here Insert Picture Description
c, escape character
Here Insert Picture Description

3.Sets (collection)

Sets (set) is a set of unordered elements will not be repeated. In addition to its main function is to repeat elements and relationships tested. Braces (set creation {} create a collection)

4.List (list)

列表由一系列按特定顺序排列的元素组成。在Python中,用方括号 [ ] 来表示列表,用使用逗号来分割其中的元素。若索引值超出范围,Python会抛出一个IndexError异常。
a、列表包容(list comprehension):是使用列表内的元素,来创建新的列表。
列表的常用操作符包括 + 和 * 。其中列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
b、列表内置的函数: 包括len()、max()、min()和list()
c、 列表的方法:
在Python解释器内输入dir([]),就可以显示这些内置方法:
Here Insert Picture Description
d、常用方法讲解:
1)append(object) 方法在列表对象的结尾添加新对象object
2)clear() 方法用于清空列表,类似于del a[:]
3)copy() 方法用于拷贝列表
4)count(value) 方法计算列表中元素对象为value的个数
5)extend(list) 方法将参数list列表中的元素加到此列表中,成为此列表的新元素
6)index(value) 方法返回列表对象值为value的索引值
7)insert(index,object) 方法在索引值为index的元素之前插入新元素object
8)pop([index]) 方法将索引值为index的元素删除,若无指定index则删除最后一个元素
9)remove(value) 方法删除列表对象的value元素,若删除元素不在列表中则会报错
10)reverse() 将列表中的元素颠倒排序
11)sort() 方法将列表对象中的元素按大小顺序排序,列表中的元素必须为同一类型

5.Tuple(元组)

元组对象不能修改,使用小括号()表示,并用逗号隔开其中的元素
元组对象的特性:
如果创建的元组对象只有一个元素,就必须在元素之后加上逗号(,),否则Python会认为此元素是要设置给变量的值:

>>>t = (1)
>>>t
1
>>>t = (1,)
>>>t
(1,)

元组的内置函数
1)len() 函数返回元组的长度。
2)max() 函数返回列表元素中的最大值。
3) min() 函数返回列表元素中的最小值。
4) tuple() 函数用于将列表转换为元组。
5) sum() 函数返回列表中所有元素的和。

6.Dictionary(字典)

字典的对象使用大括号{} 将元素列出,元素由键值(key)数值(value) 所组成,中间以冒号(:)隔开。键值必须是字符串,数字,或是元组,这些对象是不可变动的。数值则可以是任何数据类型。字典的元素排列并没有一定的顺序,因为可以使用键值来取得该元素。
a、创建字典的语法格式如下:
字典变量={关键字1:值1,关键字2:值2,……}
b、字典的内置函数
len(dict): 计算字典元素个数,即键的总数
str(dict): 输出字典以可打印的字符串表示
type(variable): 返回输入的变量类型,
c、字典的内置方法
字典对象有许多的内置方法,在Python解释器内输入dir({}),就可以显示这些内置方法的名称:
Here Insert Picture Description
d、常用方法讲解:
1)clear() 方法用于清除字典中的所有元素
2)copy() 方法用于拷贝字典
3)get(k[,d]) 方法中k是字典的键值,d是键值的默认值。若k存在则返回其值,否则返回d
Here Insert Picture Description
4)items()The method of using the element dictionary to create a (key, value) tuples target a group of
5) Key () method to create a list of objects using a dictionary key
6) popitem () method removes the first element dictionary
. 7) setDefault (k [, d]) method is a dictionary of keys k, d is the default key values. If k is present its value is returned, otherwise D
Here Insert Picture Description
. 8) Update (E) Method E to update the dictionary in the dictionary objects
. 9) values () method using a numerical key dictionary to create a list of objects

Operators

An arithmetic operator
Here Insert Picture Description
2 Comparison Operators
Here Insert Picture Description
3 assignment operator
Here Insert Picture Description
4 logical operators
Here Insert Picture Description
five operators
Here Insert Picture Description
6 identity operator
Python support for the identity of the operator and is not is.
is is determined two identifiers are not referenced from an object;
is Not the judgment identifier is not referenced from two different objects
7 member operator
Python further support member operator, test example contains a series of members, comprising strings, lists or tuples. Comprising a member operator in and Not in , y in x represents returns True if x in y sequence; x Not represented in y y if x is not the sequence returns True

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/99704833