Share the experience of "base and data type" of Python foundation!

Posted by susrisha on Mon, 31 Jan 2022 14:02:20 +0100

hello everyone! Today, xiaoqiyuan will share some basic Python (base and data types). Let's have a look~

The following is my blog, rich in content, welcome to visit!

Baby: Dream chaser (taurusblog.top) 

1, Base system

1. What is hexadecimal?

Hexadecimal, that is, the carry counting system, is a artificially defined counting method with carry (there are counting methods without carry, such as the original rope counting method, the "positive" word counting method commonly used in ticket singing, and similar tally mark counting). For any kind of base system -- X base system, it means that the number operation at each position is carried into one digit every X. Decimal is every decimal one, hexadecimal is every hexadecimal one, binary is every binary one, and so on, X-ary is every x carry. (from Baidu)

Popular explanation, the so-called base is a method of counting. How many base is to carry one bit to the high position when this base is met.

2. Binary conversion

Python can convert binary to decimal through the built-in function int(); The int() function can convert a numeric string or decimal number with specified base to an integer.

Syntax:

int(object,base)

Return value: returns integer data.

Convert binary numbers to decimal numbers

test = ['111011011111', '0b110']for number in test:
    print(int(number, 2))

Operation results:

 

Convert octal numbers to decimal numbers.

test = ['-1537202', '125']
for number in test:    print(int(number, 8))

Operation results:

 

2, Value type

1. Boolean

Boolean is actually a subtype of integer. Boolean data has only two values: True and False, which correspond to 1 and 0 of integer respectively.

Each Python object is inherently Boolean (True or False) and can be used for Boolean testing (such as if, while).

The Boolean values of the following objects are False:

Return value

type

False

(Boolean)

0

(integer 0)

0L

(long integer 0)

0.0

(floating point 0)

0.0+0.0j

(plural 0)

""

(empty string)

[]

(empty list)

()

(empty tuple)

{}

(empty dictionary)

  • An instance of a user-defined class that defines methods nonzero() or len(), and these methods return 0 or False.

  • The Boolean value of all objects except the above objects is True.

# 1. Boolean value of Python Object > > > bool (none) false > > > bool (false), bool (0), bool (0l), bool (0.0), bool (0.0 + 0.0j) (false, false, false, false, false) > > bool (''), bool ([]), bool (()), bool ({}) (false, false, false, false)
# 2. Boolean value in numerical operation True and False Corresponding to 1 and 0 of integer respectively>>> int(True), int(2 < 1)(1, 0)>>> False + 100100  #Output results>>> True + 100101  #Output results

2. Plural

The concept of complex number is exactly the same as that in mathematics. The complex number in Python has the following features:

  • The complex number is composed of real part and imaginary part, which is expressed as real+imagJ or real+imagJ.

  • The real part and imaginary part imag of a complex number are both floating-point.

>>> a = 1+2j>>> a  (1+2j)  #Output results>>> a.real # Real part 1.0     #Output results>>> type(a.real)float   #Output results>>> a.imag # Imaginary part 2.0     #Output results>>> type(a.imag)float   #Output results

3. Floating point type

Floating point type is similar to double precision floating point type in C. Floating point literals can be expressed in decimal or scientific notation, where e or e represents 10 and + (can be omitted) or - represents the plus or minus of the exponent.

>>> type(1)int   #Output results>>> type(1.0)float  #Output results>>> 1 + 1.02.0  #Output results>>> a = 1e-2>>> a  #Output result 0.01>>> type(a)float   #Output results>>> pi = 3.1415926>>> round(pi)3.0  #Output results>>> round(pi, 4)3.1416  #Output results

4. Integer

Integer is equivalent to the signed long integer in C language, which is consistent with the maximum integer of the system (for example, the integer on 32-bit machine is 32-bit, and the integer on 64 bit machine is 64 bit). The range that can be represented is limited. There are three ways to represent integer literals: decimal (common), binary (starting with "0b"), octal (starting with the number "0"), and hexadecimal (starting with "0x" or "0x").

>>> a = 0b10100 >>> type(a)int   #Output results>>> a20  #Output results>>> bin(20), oct(20), hex(20)('0b10100', '024', '0x14')  # Output results

5. string (string)

#!/usr/bin/env python3

str1 = 'Hello'

print(str1[0])

The string needs to be enclosed in single quotation marks' 'or double quotation marks'', and three quotation marks - comments
String is also a special tuple. You cannot change the value of an element in a string
String type operation: + connect multiple strings * repeatedly output the string n times. The string name [:] intercepts part of the string (the leftmost index value is 0, the rightmost index value is - 1, the penultimate index value is - 2...) string name [] Obtain a single character in/not in the string through the index, judge whether a single character is / is not in the string, r/R, and treat special characters such as escape characters as ordinary characters
Basic operations: index, slice, multiply - multiple output, membership check, length len(), maximum, minimum

6. Long integer

Long integers are supersets of integers and can represent infinite integers. Long literals are followed by the letter "L" or "L" (capitalized "L").

>>> a = 999 ** 8  # Automatic conversion of integer to long > > > a8920457944069944027201l > > > type (a) long  

 -  Copyright notice: unless otherwise stated, the copyright of all articles on this blog belongs to the author. Reprint please indicate the source!

 

Topics: Python Pycharm IDE