Summary of the ten basic concepts of Python

Python is an interpreted language. Python uses indentation alignment to organize code execution, so code without indentation will be automatically executed when loaded

One, the data type

Python supports three different numeric types:

type keywords Ranges
plastic int Unlimited
floating point float decimal
plural complex Composed of real and imaginary numbers

There are 6 standard data types in Python:

  1. Number
  2. String(string)
  3. List (list)
  4. Tuple (tuple)
  5. Sets
  6. Dictionart (dictionary)

Among them the immutable data:

  • Number
  • String(string)
  • Tuple (tuple)
  • Sets

can become:

  • List (list)
  • Dictionart (dictionary)

We can use type or isinstance to determine the type

class A:
	pass

class B:
	pass

print(isinstance(A(), A));
print(type(A()) == A);

print(isinstance(B(), A));
print(type(B()) == A);

The output is:
True
True
False
False

type() does not consider a subclass to be a superclass type.
isinstance() will consider the subclass to be a superclass type


Second, variables

To define a variable in python, you do not need to write the variable type, but it must be initialized.

Python will automatically match the variable naming rules according to the data type we write.
It consists of letters, numbers, and underscores. The first one must be a letter or underscore.

Input and output
When we need to input Chinese, we need to include the header file# - - coding: UTF-8 - - or #coding=utf-8

  • Input a = input("Please enter...") The return value is str type
  • output print('hello world')

Third, the string

Python's Strings
Now that we've figured out the dreaded character encoding problem, let's look at Python's strings.

In the latest Python 3 version, strings are encoded in Unicode, that is, Python strings support multiple languages. For the encoding of a single character, Python provides the ord() function to obtain the integer representation of the character, chr() The function converts the encoding to the corresponding character:


Fourth, the operator

The python operator reports an error:

  • Arithmetic operators: *, , /, //, +, ( : for power, //: for division)
  • Logical operators: and,or,not and, or, not
  • Assignment operator: =, and the combination of the above arithmetic operator and =, such as: +=, -=
  • Identity operator: is is not


V. List

Lists are the most basic data structure in Python.
Each value in the list has a corresponding position value, called an index, the first index is 0, the second index is 1, and so on.

define a list

list1 = [1, 2, 3]
list2 = [1, 2, '3']

access list


Six, tuple

Python tuples are similar to lists, except that the elements of the tuple cannot be modified. Use parentheses ( ) for tuples and square brackets [ ] for lists.

Tuple creation is as simple as adding elements in parentheses and separating them with commas.

tup1 = () # 空元组
tup2 = (1, 2, '3') 

tup3 = tup1 + tup2 # 元组求和

del tup1 # 删除元组

Seven, dictionary

Dictionaries are another mutable container model and can store objects of any type. keys must be unique

Each key-value key=>value pair of the dictionary is separated by a colon: , and each pair is separated by a comma (,). The entire dictionary is enclosed in curly braces {}. The format is as follows:

dic = {
    
    key1 : value1, key2 : value2}

Definition and Paradigm Dictionary

dict = {
    
    'Name': 'python'}
print ("dict['Name']: ", dict['Name'])

# 输出:dict['Name']:  python

Eight, collection

A set is an unordered sequence of non-repeating elements.

Sets can be created using curly braces { } or the set() function. Note: To create an empty set, you must use set() instead of { }, because { } is used to create an empty dictionary.

Create syntax:

gather = {
    
    value1,value2}
# 或者
gather set(value)

Basic operation:

# 定义
gather = (1,2,3,4,5)

# 添加
gather.add(6)

# 移除
gather.remove(1)

# 随机移除一个元素
gather.pop() 

# 计算元素个数
len(gather)

# 清空集合元素
gather.clear()

# 判断元素是否存在
2 in gather

Nine, branch structure

if-else
if-elif-else (else can be omitted here)

Logical result:

  • Anything that is "empty" in python is false
  • "" (write nothing as false, write anything as true)
  • Empty tuple, empty list, empty dictionary, 0 are all false

Example:

a = 1
b = 1

if a < b:
	print("a小于b")
elif a==b:
	print("a等于b")
else:
	print("a大于b")

Ten, loop structure

Looping statements in Python are for and while.

  • while loop
    The general form of a while statement in Python:
while 判断条件(条件):
    执行语句()……

Example: Calculate a sum of 1-100

n = 100 
sum = 0
counter = 1
while counter <= n:
    sum = sum + counter
    counter += 1
 
print("1 到 %d 之和为: %d" % (n,sum))

  • The while loop uses the else statement
    If the conditional statement following the while is false, the block of the else statement is executed.

grammar:

while 判断条件(条件):
    执行语句()……
else:
    执行语句()……

Combining the above example:

  • for statement
    Python for loops can iterate over any iterable object, such as a list or a string.

The general format of a for loop is as follows:

for <variable> in <sequence>:
    <statements>
else:
    <statements>

Example:

arr = [1,2,3,4,5]
for x in arr:
	print (x)

Guess you like

Origin blog.csdn.net/Czhenya/article/details/121726906