Introduction to the
The main use of Python is for scientific calculations, which are based on numbers, strings, and lists. This article will give you a detailed introduction to the use of these three data types.
digital
Numbers are a very important type in any scientific computation, and the most common numeric types in Python are int and float.
Take a look at some basic numeric manipulation:
In [8] : 1 + 1 Out [8] : 2 In [9] : 3 * 2 + 10 Out [9] : 16 In [10] : (65 + 23) / 4 Out [10] : 22.0Copy the code
As you can see above, those without decimals are int and those with decimals are float.
The division operation (/) always returns a floating-point type. If you want to do floor division and get an integer result (ignoring the decimal part) you can use the // operator; If you want to calculate the remainder, you can use %
In [11]: 54/4 Out[11]: 13.5 In [12]: 54 // 4 Out[12]: 13 In [13]: 54%4 Out[13]: 2Copy the code
** can represent a power operation:
In [14]: 4 ** 3
Out[14]: 64
Copy the code
We can assign numeric operations to specific variables and use that variable for subsequent operations.
In [15]: a = 12
In [16]: b = 14
In [17]: a * b
Out[17]: 168
Copy the code
In an interactive environment, _ represents the previous output:
In [17]: a * b
Out[17]: 168
In [18]: 100 + _
Out[18]: 268
Copy the code
In addition to int and float, Python supports other data types, such as Decimal and Fraction, and even complex numbers.
string
There are three representations of strings in Python, using single, double, and triple quotes.
In [19]: site1 = 'www.flydean.com'
In [20]: site2= "www.flydean.com"
In [21]: site3= """www.flydean.com"""
Copy the code
The triple quotation marks are mainly used for cross-line output. The carriage return newline in the string is automatically included in the string. If you do not want to include the newline, add \ to the end of the line. As follows:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
Copy the code
You can use the backslash \ if you want to escape
In [22]: site4 = "www.\"flydean\".com"
In [23]: site4
Out[23]: 'www."flydean".com'
Copy the code
If you don’t want the character prefixed with \ to be escaped as a special character, you can use the raw string style and add r before the quotation mark:
In [24]: print(r"www.\"flydean\".com")
www.\"flydean\".com
Copy the code
Strings are concatenated with +, or copied with * :
In [25]: "www" + "flydean.com"
Out[25]: 'wwwflydean.com'
In [26]: "www.flydean.com" * 3
Out[26]: 'www.flydean.comwww.flydean.comwww.flydean.com'
Copy the code
Two or more adjacent string literals (quoted characters) are automatically concatenated together.
In [27]: "www" "flydean.com"
Out[27]: 'wwwflydean.com'
Copy the code
Note that the autoconcatenation operation above only works for two literals, and an error is reported if the literals are variables.
A string is treated as an array of characters, so it can be accessed as string[index].
In [28]: site5 = "www.flydean.com"
In [29]: site5[3]
Out[29]: '.'
Copy the code
If the index is negative, it counts from the right:
In [30]: site5[-3]
Out[30]: 'c'
Copy the code
Since negative 0 is the same thing as 0, negative numbers start at negative 1.
In addition to indexing, strings also support slicing. Indexes can get single characters, while slicing can get substrings:
In [31]: site5[1:5]
Out[31]: 'ww.f'
Copy the code
Note that the beginning of the slice is always included in the result, but the end is not. This makes s[: I] + s[I :] always equal s
In [33]: site5[:4]+site5[4:]
Out[33]: 'www.flydean.com'
Copy the code
The index of the slice has a default value, which defaults to 0 when the index is omitted.
An out-of-bounds error is sent if the index is outside the range of the string.
In [34]: site5[100] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-34-fc1f475f725b> in <module>()----> 1 site5[100]
IndexError: string index out of range
Copy the code
However, out-of-bounds indexes in slices are handled automatically:
In [36]: site5[:100]
Out[36]: 'www.flydean.com'
Copy the code
Since strings are immutable, we cannot modify strings by indexing them:
In [37]: site[2] = "A"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-9147d44bd80c> in <module>()
----> 1 site[2] = "A"
TypeError: 'str' object does not support item assignment
Copy the code
Len counts the length of the string:
In [38]: len(site5)
Out[38]: 15
Copy the code
String object STR
The essence of a string is the string object STR.
Take a look at the basic method of STR:
In [39]: site5.
capitalize() encode() format() isalpha() islower() istitle() lower() replace() rpartition() splitlines() title()
casefold() endswith() format_map() isdecimal() isnumeric() isupper() lstrip() rfind() rsplit() startswith() translate()
center() expandtabs() index() isdigit() isprintable() join() maketrans() rindex() rstrip() strip() upper()
count() find() isalnum() isidentifier() isspace() ljust() partition() rjust() split() swapcase() zfill()
Copy the code
If you’re interested, you can do your own research.
The list of
A list is a collection of data represented by square brackets. The data in a list can be of many data types, but generally we use the same data type in a list.
In [40]: ages = [ 10, 14, 18, 20 ,25]
In [41]: ages
Out[41]: [10, 14, 18, 20, 25]
Copy the code
Like strings, lists support indexing and slicing. In fact, indexes and slicing are supported for any data type of sequence type.
In [42]: ages[3]
Out[42]: 20
In [43]: ages[:2]
Out[43]: [10, 14]
In [44]: ages[:]
Out[44]: [10, 14, 18, 20, 25]
Copy the code
Notice that slicing the list returns a new list. But the new list is a shallow copy, meaning that the elements of the new list are references to elements in the original list.
Lists also support concatenation operations:
In [45]: ages + [9, 11]
Out[45]: [10, 14, 18, 20, 25, 9, 11]
Copy the code
Unlike String immutability, lists are mutable, which means we can change the value of a list by indexing it:
In [46]: ages[0] = 100
In [47]: ages
Out[47]: [100, 14, 18, 20, 25]
Copy the code
The underlying type of a list is a list. We can look at the methods in a list:
In [51]: ages.
append() count() insert() reverse()
clear() extend() pop() sort()
copy() index() remove()
Copy the code
We can append the value of the list, count the number of elements in the list, and so on.
As mentioned above, the slice of the list is a reference to the original list, so we can change the value of the original list by assigning a value to the slice:
>>> letters = ['a'.'b'.'c'.'d'.'e'.'f'.'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C'.'D'.'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
Copy the code
Lists can also be nested to build multi-tier lists:
>>> a = ['a'.'b'.'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
Copy the code
This article is available at www.flydean.com/03-python-n…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!