There is no end to learning, the most basic content in Python learning

There is no end to learning, the most basic content in Python learning

  • Interpreted language, can run without compiling
  • Provides interactive command line
  • Object-based programming ideas
  • Cross-platform and good compatibility, can run on Windows, Mac, Linux
  • Simple to use and powerful

01 Chinese encoding

Many students will encounter garbled characters when opening data. The reason is the encoding problem of the character set. The default encoding set for Linux and Mac is UTF8, while Windows is ASCII. If the character set of the data encoding is different from the character set used when you use Python for processing, garbled characters will occur.

In addition, my personal habit is to add the following content to the head of the Python code, where the second line declares the use of the UTF8 character set.

#!/usr/bin/env python# coding:utf8

02 Variable

Variables in Python can be seen as a container, which stores the values ​​we need to use.

Python has the same requirements for variable names as other languages: English, numbers, and underscores can be included, but they cannot start with numbers, and are case sensitive. Of course, I recommend that the variable names are in plain English, and some meaningful names should be used to facilitate their own understanding of the role of each variable.

Python is a weakly typed language and there is no need to declare its type when using variables. Variables in Python include the following categories: numeric values, strings, lists, tuples, and dictionaries.

03 Numerical value

Numerical values ​​include integer and floating-point types, which correspond to integers and floating-point numbers, respectively, with the latter having higher precision.

# 整型a = 1# Floating point b = 2.1print a, b

04 String

Strings are texts that we often come into contact with. You can put content of any length in them, enclosed in single or double quotation marks. It should be noted that Chinese and Chinese symbols can only appear in the string. If the comma of the Chinese input method is used in the third line below , Python will report an error.

c = Hello d = Hello print c, d

Use + to concatenate two strings.

print c + d

Use len() to get the length of the string.

print len( Hello World)

Use slices to access a character or a fragment in a string.

# The position subscript starts from 0 c = Hello World # The print result is H, and the subscript is 0 for the first character print c[0]
# The print result is d, and the subscript is negative, which means counting from back to front # So -1 means the first character from the bottom print c[-1]
# Use: return a fragment, before and after the colon are the start subscript and the end subscript.
# Therefore c[1:5] means to return the fragments with subscripts from 1 to 4, that is, the second to fifth characters print c[1:5]
# Subscripts before and after the colon can also use negative numbers
# Or not provided, which means starting from the left end or all the way to the right end print c[1:-1], c[:5], c[3:]
05 list

The list is like a team, which stores multiple variables in sequence. Lists are similar to strings, but each element in the string is a character, and each element in the list can be any type of variable.

# Use [] to define an empty list, and use append() to add an element to the end of the list
# If you want to add to the header, just use prepend() a = []a.append(1)a.append(2.1)a.append( Hello )print a

Use len() to get the length of the list.

print len(a)

The operations such as index access and assignment of list elements are similar to strings.

print a[1], a[-1]a[1] = 100print a

Use del to delete an element in the list.

del a[0]print a

06 tuple

A tuple is similar to a list. The only difference is that the elements in a tuple cannot be changed after initialization, so it can be understood as a read-only variable.

# Use () to define a tuple a = (1, 2.1, Hello )# Trying to modify the elements in the tuple will report an error a[0] = 100
07 Dictionary

A dictionary is an extremely important variable type. A key is used to access the corresponding value, which is a data form of key-value pairs.

# Use {} to define a dictionary a = {}# Use key to assign valuea[ k1] = 1a[ k2] = 2.1a[ k3] = Hello

So you can summarize the difference between a dictionary and a list. The elements in the list are ordered and equivalent, so subscripts are used for assignment and access, while the elements in the dictionary are unordered, so the key is used to manipulate the corresponding value.

# You can also assign values ​​while defining dictionaries and lists li = [1, 2.1, Hello ]di = {k1: 1, k2: 2.1, k3: Hello}

Use has_key() to determine whether there is a key in the dictionary.

print di.has_key( k4)

If you access a key that does not exist, Python will report an error. When assigning, if the key already exists, the existing value will be overwritten with the new value.

08 comments

The annotated code will not run, it can be seen as some notes and instructions for yourself and other programmers to read, to improve the readability of the code.

# Here is a single-line comment, here is a multi-line comment

In Sublime, select the content to be annotated and press Ctrl+/to complete the annotation.

09 reserved characters

In Python, some strings have certain functions, such as import, class, etc. When choosing variable names, we should avoid these reserved characters.

# The following variable assignment will report an error import = 1

10 lines and indentation

In Python, the boundaries of code blocks are not explicitly divided by symbols such as braces, but by line indentation. Codes with the same continuous indentation level are in the same code block. When using syntax such as for, while, if, try, you need to pay attention to the indentation of each line of code.

Reference: https://cloud.tencent.com/developer/article/1520683 There is no end to learning, the most basic content in Python learning-Cloud + Community-Tencent Cloud