Python learning-variable types

Reference link: Print single variable and multiple variables in Python

  1. Single variable assignment

The equal sign (=) is used for assignment. The left side is a variable name, and the right side is the value stored in the variable. The definition of a variable does not need to declare the type, and it can be directly assigned.

example:

temp1 = 100 # Assign an integer variable

temp2 = 10234.0234 # floating point

temp3= "The weather is good!" # String

  1. Multivariate assignment

Python supports assigning values to multiple variables.

example:

a=b=c=d=1

Python also supports simultaneous assignment to multiple variables of different types.

example:

temp1,temp2,temp3 = 123,22.424,"The weather is okay"

  1. Data type and number type

Python has five standard data types:

Numbers (number) String (string) List (list) Tuple (tuple) Dictionary (dictionary)

Python supports four different number types:

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

example:

intlongfloatcomplex1051924361L0.03.14j100-0x19323L15.2045.j-7860122L-21.99.322e-36j0800xDEFABCECBDAECBFBAEl32.3e+18.876j-0490535633629843L-90.-.6545+0J-0x260-052318172735L-32.54e1003e+26J0x69-4721885298529L70.2E-124.53e-7j

The representation of a complex number can be a + bj, or complex(a,b). The long type seems to be no longer used in Python3.

  1. String

Strings are very commonly used in Python.

Python's string list has 2 order of values:

The index from left to right starts at 0 by default, and the maximum range is the string length less than 1. The index from right to left starts at -1 by default, and the maximum range is the beginning of the string

String interception:

The character string is intercepted by means of the character string variable [head subscript: tail subscript].

example:

str = "12345678"

a = str[0]

b = str[7]

c = str[-1]

d = str[-3]

e = str[2:]

f = str[:4]

g = str[1:5]

h = str[3:-1]*2

i = str[-5:-1]+'0000'

print(a,b,c,d,e,f,g,h,i)

The output is:

1 8 8 6 345678 1234 2345 45674567 45670000

str[0] means to take the first bit of the string

str[7] means take the eighth place

str[-1] means to take the last digit, which is the first to last

str[-3] means take the third place from the bottom

str[2:] means to take from the third position to the end

str[:4] means to take from the beginning to the third

ps: The beginning is from 0. When intercepting with the string variable [head subscript: tail subscript], it ends at one bit before the tail subscript, excluding the tail subscript.

str[1:5] means to start from the second to the fourth end

str[3:-1] means starting from the fourth digit to ending with the penultimate digit, *2 means outputting 2 times

str[-5:-1] means from the fifth to last digit, and then concatenate 0000

  1. Python list

Lists support characters, numbers, strings and can even contain lists (ie nesting).

The list is marked with [], which is the most common compound data type in python.

The cutting of the value in the list can also use the list variable [head subscript: tail subscript] to intercept the corresponding list. The default index from left to right starts at 0, and the index from right to left defaults to start at -1. Subscripts can be Empty means to get to the beginning or end.

example:

list = [123,"apple",33.23]

new_list = [222,"banana",11.65,112,list]

print(list)

print(new_list)

print(new_list[0])

print(new_list[-1])

print(new_list[:-1])

print(new_list[1:3]*2)

Output:

[123, ' apple', 33.23]

[222, ' banana', 11.65, 112, [123, 'apple', 33.23]]

222

[123, ' apple', 33.23]

[222, ' banana', 11.65, 112]

[' banana', 11.65, 'banana', 11.65]

The interception operation of the list is the same as the operation of the string.

  1. Python tuple

Tuples are another data type, similar to List.

Tuples are identified by (), and internal elements are separated by commas. However, tuples cannot be assigned twice, which is equivalent to a read-only list and cannot be changed after assignment.

So like:

tuple = (123,"apple",33.23)

tuple(2)=3

An error will be reported: SyntaxError: can't assign to function call syntax error.

example:

tuple = (123,"apple",33.23)

new_tuple = (1,"banana",2.2,3,tuple)

print(tuple)

print(new_tuple)

print(new_tuple[0])

print(new_tuple[-1])

print(new_tuple[:-1])

print(new_tuple[1:3])

The interception of tuples is the same.

result:

(123, ' apple', 33.23)

(1, ' banana', 2.2, 3, (123, 'apple', 33.23))

1

(123, ' apple', 33.23)

(1, ' banana', 2.2, 3)

(' banana', 2.2)

  1. Python dictionary

Dictionary (dictionary) is the most flexible built-in data structure type in Python besides lists. A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.

The difference between the two is that the elements in the dictionary are accessed through keys, not offsets. The dictionary is identified by "{ }". The dictionary consists of an index (key) and its corresponding value.

example:

dict = {}

dict['one'] = "This is one"

dict[2] = "This is two"

tinydict = {'name': 'runoob', 'code': 6734, 'dept': 'sales'}

print(dict['one']) # output the value of the key as'one'

print(dict[2]) # output the value of key 2

print(tinydict) # output complete dictionary

print(tinydict.keys()) # output all keys

print(tinydict.values()) # output all values

result:

This is one

This is two

{' name': 'runoob', 'code': 6734, 'dept': 'sales'}

dict_keys(['name', 'code', 'dept'])

dict_values(['runoob', 6734, 'sales'])

  1. Force data type conversion

Function description int(x [,base]) convert x to an integer long(x [,base]) convert x to a long integer float(x) convert x to a floating point number complex(real [,imag]) Create a complex number str(x) to convert the object x to a string repr(x) to convert the object x to an expression string eval(str) is used to calculate the valid Python expression in the string and returns an object tuple( s) Convert the sequence s into a tuple list(s) Convert the sequence s into a list set(s) Convert into a variable set dict(d) Create a dictionary. d must be a sequence (key, value) tuple. frozenset(s) is converted to an immutable set chr(x) an integer is converted to a character unichr(x) an integer is converted to a Unicode character ord(x) a character is converted to its integer value hex(x) is a Convert an integer to a hexadecimal string oct(x) Convert an integer to an octal string

Both str() and repr() can convert objects in python to string types, but there are differences. str is for users, while repr is for programmers.

If you don’t understand, give a chestnut:

s = 'abcde'

print(str(s))

print(repr(s))

The output is:

abcde

' abcde'

I found that the repr method added quotation marks, try again.

s = 'abcde\n'

print(str(s))

print(repr(s))

abcde

' abcde\n'

The repr method does not recognize the \n line break, but directly outputs the \n.

try again:

s = 'abcde\'

print(str(s))

print(repr(s))

abcde\

' abcde\'

You can find the difference between repr and str, which can be called as required.

notes:

Variable assignment is simple and rude, no need to declare type, flexible and very easy to use. The digital data type is an unchangeable data type. Changing the digital data type will allocate a new object. The operation of the string has basic functions and does not require the operation of splicing and traversal by itself. The list uses "[ ]" to identify arrays similar to C language. Tuples are identified by "( )". Internal elements are separated by commas. But tuples cannot be assigned twice, which is equivalent to a read-only list. The dictionary is identified by "{ }". The dictionary consists of the index key and its corresponding value value. To determine the data type, you can use the type() function, or you can use isinstance(a, int) to verify

Recommended Posts

Python learning-variable types
02. Python data types
Python basic data types
Python basic data types
Detailed explanation of python sequence types
Python basic syntax and number types
Python CookBook
Python FAQ
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Comprehensive summary of Python built-in exception types
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
Can variable types be declared in python
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Detailed explanation of data types based on Python