Must-see content for beginners (2)

Must-see content for beginners (2)

1. Variable assignment

  • Variable assignment in Python does not require a type declaration.
  • Each variable is created in memory, including the variable's identifier, name, and data.
  • Each variable must be assigned a value before it is used, and the variable will not be created until the variable is assigned.
  • The equal sign (=) is used to assign values ​​to variables.
  • The left side of the equals (=) operator is a variable name, and the right side of the equals (=) operator is the value stored in the variable.

2. Standard data types

  • The data stored in memory can be of many types.
  • For example, a person's age can be stored in numbers and his name can be stored in characters.
  • Python defines some standard types for storing various types of data.
  • Python has five standard data types:

       Numbers

       String (string)

       List

      Tuple (tuple)

      Dictionary (dictionary)

3. Python Numbers

  • Numeric data types are used to store numeric values.
  • They are immutable data types, which means that changing the numeric data type will allocate a new object.
  • Python supports four different numeric types:

       int (signed integer)
       long (long integer [can also represent octal and hexadecimal])
       float (floating point)

       complex (plural)

4. Python String

  • A string or string (String) is a string of characters consisting of numbers, letters, and underscores.
  • Generally recorded as:
s="a1a2···an"(n>=0)

It is a data type in programming languages ​​that represents text.

Python's string list has 2 value orders:

  • From left to right, the default index starts from 0, and the maximum range is 1 less than the length of the string
  • Right-to-left indexing starts from -1 by default, the maximum range is the beginning of the string

If you want to obtain a substring from a string, you can use the variable  [head subscript: tail subscript] to intercept the corresponding string, where the subscript starts from 0 and can be a positive number or Negative numbers, the subscript can be empty, which means to get the head or tail.

for example:

s = 'ilovepython'

The result of s[1:5] is love.

When using a colon-separated string, python returns a new object containing the contiguous content identified by the pair of offsets, starting on the left and including the lower bound.

The above result includes the value l of s[1], and the maximum range obtained does not include the upper boundary, which is the value p of s[5].

The plus sign (+) is the string concatenation operator, and the asterisk (*) is the repeat operation. The following example:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- str = ' Hello World! ' print str # Print the complete string print str [ 0 ] # print the first character in the string print str [ 2 : 5 ] # prints the third to fifth strings in the string print str [ 2 : ] # print the string starting at the third character print str * 2 # print the string twice print str + " TEST " # 输出连接的字符串

以上实例输出结果:

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324752713&siteId=291194637
Recommended