home PYTHONJAVA
 

Python3 basic data type

Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will be created after the variable is assigned.

In Python, a variable is a variable. It has no type. The "type" we refer to is the type of the object in memory that the variable refers to.

The equal sign (=) is used to assign a value to a variable.

The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable. E.g:

Instance (Python 3.0+)

#!/usr/container/python3 counter = 100 # Integer Variable miles= 1000.0 # gliding point variable name= "Welookups" # string print (counter) print (miles) print (name)

Number(number)

Python3 supports int, float, bool, complex (plural) ..

In Python 3, there is only one integer type int , which is represented as a long integer, and there is no Long in python2.

Like most languages, the assignment and calculation of numeric types is straightforward.

The built-in type() function can be used to query the type of object pointed to by the variable.

>>> a, b, c, d = 20, 5.5, True, 4+3j 
>>> print(type(a), type(b), type( c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

In addition, you can use isinstance to judge:

Instance...

>>>a = 111 >>> isinstance(a, int) True >>>

The difference beTruen isinstance and type is:

  • Type() does not consider a subclass to be a parent class type.
  • Isinstance() considers a subclass to be a parent class type.
>>> class A:
... pass
...
>>> class B(A):
... pass
...
>>> isinstance(A(), A)
True
>>> type(A()) == A
True
>>> isinstance(B(), A)
True
>>> type(B()) == A
False

Note: TThere is no boolean in Python 2, it uses the number 0 for False and 1 for True. In Python 3, True and False are defined as keywords, but their values ​​are still 1 and 0, which can be added to the numbers.

Numerical operations

Instance

...
>>>5 + 4 # addition 9 >>> 4.3 - 2 # Subtraction 2.3 >>> 3 * 7 # Subtraction multiplication 21 >>> 2 / 4 # Divide, get a floating point number 0.5 >>> 2 // 4 # Divide, get an integer 0 >>> 17 % 3 # Residual 2 >>> 2 ** 5 # Take the margin 32

Note:

  • 1, Python can assign values to multiple variables at the same time, such as a, b = 1, 2.
  • 2. A variable can be assigned to a different type of object by assignment.
  • 3. The division of values consists of two operators: / returns a floating point number, // returns an integer.
  • 4. In mixed computing, Python converts integers to floating point numbers.

Value type instance

intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3e+18.876j
-0490-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2E-124.53e-7j

Python likewise underpins complex numbers. The mind boggling number comprises of a genuine part and a fanciful part. It very well may be spoken to by a + bj, or complex(a,b). The genuine section an and the fanciful part b of the unpredictable number are both drifting point types


String(string)

Strings in Python are encased in single statements ' or twofold statements " with an oblique punctuation line \ Escape unique characters.

The grammar of the capture attempt of a string is as follows:

variables [header subscript: tail subscript]

The file esteem begins with 0 and - 1 is the begin position from the end.

The in addition to sign + is the string's connector, and the bullet * implies replicating the present string, trailed by the number The seasons of duplicating. A precedent is as follows:

Instance

#!/usr/bin/python3 str = 'Welookups' print (str) # Output string print (str[0:-1]) # Output all characters from the first to the second last print (str[0]) # Output string first character print (str[2:5]) # Output characters from the third to the fifth print (str[2:]) # Output all characters after the third start print (str * 2) # Output string string twice... print (str + "TEST") # Connection string

Executing the above program will output the following results:

Welookups
Weloo  
w
loo
welo
WelookupsWelookups
    WelookupsTEST

Python uses a backslash (\) to escape special characters. If you don't want the backslash to be escaped, you can add an r to the front of the string to represent the original string:

>>> print('We\lookups')
We
loo
>>> print(r'We\lookups')< /span>
We\lookups
>>> 

Also, the oblique punctuation line (\) can be utilized as a continuation character, demonstrating that the following line is a continuation of the past line. You can likewise utilize """...""" or '''......''' to traverse various lines.

Note that Python does not have a different character type, a character is a string of length 1.

Instance

>>>word = 'Python' >>> print(word[0], word[5]) P n >>> print(word[-1], word[-6]) n P
rf

Instance

#!/usr/bin/python3 list = [ 'abcd', 786 , 2.23, 'Welookups', 70.2 ] tinylist = [123, 'Welookups'] print (list) # Output complete list print (list[0]) # Output list first element print (list[1:3]) # Output from the second to the third element print (list[2:]) # Output all elements starting with the third element... print (tinylist * 2) #Output twice list print (list + tinylist) # Connection list

The output of the above example:

['abcd', 786, 2.23, 'Welookups', 70.2]
abcd
[786, 2.23]
[2.23, 'Welookups', 70.2]
[123, 'Welookups', 123, 'Welookups']
['abcd', 786, 2.23, 'Welookups', 70.2, 123, 'Welookups']

Unlike Python strings, the elements in the list can be changed:

Instance

>>>a = [1, 2, 3, 4, 5, 6] >>> a[0] = 9 >>> a[2:5] = [13, 14, 15] >>> a [9, 2, 13, 14, 15, 6] >>> a[2:5] = [] # Set the corresponding element value to [] >>> a [9, 2, 6]

List has a ton of worked in strategies, for example, attach(), pop(), and so on., which will be secured later.

Note:

  • 1, List is written beTruen square brackets, elements are separated by commas.
  • 2. Like strings, lists can be indexed and sliced.
  • 3, List can be spliced ​​using the + operator.
  • 4.The elements in the List can be changed.

he Python list interception can receive the third parameter, the parameter is the intercepted step size, the following instance is in the position of index 1 to index 4 and set to a step size of 2 (interval by one position) to intercept the string:


Tuple (tuple)

Tuples are like records, then again, actually the components of the tuple can't be changed. Tuples are written in brackets () with components isolated by commas.

The component types in the tuple can likewise be unique:

Instance

#!/usr/bin/python3 tuple = ( 'abcd', 786 , < /span>2.23, 'Welookups', 70.2 ) tinytuple = (123, 'Welookups') print (tuple) # Output complete tuple print (tuple[0]) # The first element of the output tuple print (tuple[1:3]) # The output starts from the second element to the third element print (tuple[2:]) # Output all elements starting with the third element print (tinytuple * 2) # Output two tuples print (tuple + tinytuple) # Connection tuple

The above example output:

('abcd', 786, 2.23, 'Welookups', 70.2)
abcd
(786, 2.23)
(2.23, 'Welookups', 70.2)
(123, 'Welookups', 123, 'Welookups')
('abcd', 786, 2.23, 'Welookups', 70.2, 123, 'Welookups')

Instance

>>>tup = (1, 2, 3, 4, 5, 6) >>> print(tup[0]) 1 >>> print(tup[1:5]) (2, 3, 4, 5) >>> tup[0] = 11 # Modifying tuple elements is illegal Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>>

Although the tuple's elements cannot be changed, it can contain mutable objects, such as a list of lists.

ConstWecting a tuple containing 0 or 1 elements is special, so there are some additional grammar Weles:

tup1 = () # Empty group
tup2 = (20,) # An element,Need to add a comma after the element

string, rundown, and tuple are for the most part groupings.

Note:

  • 1. Like a string, the components of a tuple can't be altered.
  • 2. Tuples can likewise be recorded and cut similarly.
  • 3. Note the exceptional language structure Weles for constWecting tuples containing 0 or 1 components.
  • 4. The tuple can likewise be grafted utilizing the + administrator.

Set(collection)

A set is made out of one or a few distinct shapes, and the things or articles that make up the set are called components or individuals.

The fundamental capacity is to perform participation testing and expel copy components.

You can create a collection using braces { } or set() functions. Note: Creating an empty collection must use set() instead of { } because { } is used to create An empty dictionary.

Create a format:

Instance

#!/usr/bin/python3 student = {' Tom', 'Jim' , 'Mary' , 'Tom', ' Jack', ' Rose'} print(student) # Output collection, duplicate elements are automatically removed # member test if 'Rose' in student : print('Rose in the collection') else : print('Rose is not in the collection') # set can perform set operations a= set('abracadabra') b= set('alacazam') print(a) print(a - b) # difference beTruen a and b print(a | b) #The union of a and b print(a & b) #The intersection of a and b print(a ^ b) #a and b elements that do not exist at the same time

The output of the above example:

{'Mary',  'Jim', 'Rose', 'Jack', 'Tom'}
Rose in the collection
{'b', 'a', 'c', 'r', 'd' }
{'b', 'd', 'r'}
{'l', 'r', 'a', 'c', 'z' , 'm', 'b', 'd'}
{'a', 'c'}
{'l', 'r', 'z', 'm', 'b' , 'd'}

Dictionary

A dictionary is another very useful built-in data type in Python.

Lists are ordered collections of objects, and dictionaries are collections of unordered objects. The difference beTruen the two is that the elements in the dictionary are accessed by keys rather than by offset access.

The dictionary is a type of mapping, the dictionary is identified by { }, which is an unordered key: value Collection.

The

key must use an immutable type.

In the same dictionary, the key must be unique.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.