Click the above “Beginner Learning Vision” to select “Star” or “Top”
Important content delivered to you first
It has been a while since I last wrote Python. Today, I will introduce the data object types in Python and the basics of Python programming, including conditional statements and loops. Just like the vector, data frame, and factor data object types in R, Python’s basic data types include list, tuple, dict, and set. Additionally, I will briefly introduce if-else conditional statements, for and while loops, and other basic programming concepts in Python.
>>>>
Python Data Types
The main data types in Python include objects such as list, tuple, dict, and set. Below, I will introduce these Python data types one by one.
The list is a built-in data type in Python, serving as an ordered collection of data. The elements of a list are mutable, allowing for arbitrary additions or deletions of elements within the list. Run the following list-related code in the Python interactive shell:
>>> NBAplayer = ['Westbrook', 'Harden', 'Durant']
>>> NBAplayer
['Westbrook', 'Harden', 'Durant']
The object NBAplayer is a list, and we can use indexing to access each element in the list. In Python, indexing starts from 0, which differs from R:
>>> NBAplayer[0]
'Westbrook'
>>> NBAplayer[2]
'Durant'
You can also access each object in the list in reverse order:
>>> NBAplayer[-1]
'Durant'
To add an object to the list, you can use the append method:
>>> NBAplayer.append('James')
>>> NBAplayer
['Westbrook', 'Harden', 'Durant', 'James']
To delete an object from the list, you can use the pop method:
>>> NBAplayer.pop(1)
'Harden'
>>> NBAplayer
['Westbrook', 'Durant']
A list can also be included as a single element within another list:
>>> player = ['Curry', 'Leonard']
>>> NBAplayer = ['Westbrook', 'Harden', player, 'Durant']
Next, let’s look at another important data type in Python: tuple. Tuples are very similar to lists, except that tuples are enclosed in parentheses and cannot be modified freely after initialization like lists.
>>> NBAplayer = ('Westbrook', 'Harden', 'Durant')
>>> NBAplayer
('Westbrook', 'Harden', 'Durant')
Tuples have the same object element access functionality as lists, which will not be elaborated on here. It is important to note that since tuple elements are immutable, there are no methods like append or pop for modifying elements.
Finally, let’s look at a special data type in Python: dict (dictionary). As the name suggests, dictionaries have powerful data querying capabilities. A dict, known as a map in other programming languages, has a key-value storage functionality. See the example below:
>>> NBAplayer = {'Westbrook': 32.3, 'Harden': 29.9}
>>> NBAplayer['Westbrook']
32.3
In addition to specifying each element’s key-value when creating a dict, you can also specify values individually using the key:
>>> NBAplayer['Durant'] = 25.7
>>> NBAplayer['Durant']
25.7
Dicts are extremely fast for data lookup or insertion, but they also consume a lot of memory, which is the opposite of lists. Another data type similar to dict is set, which is a collection of keys without storing values; this will not be covered here.
>>>>
Python Programming Fundamentals
This section mainly introduces if-else conditional statements and for and while loops. As foundational concepts in any programming language, it is necessary to emphasize conditional statements and loops here. First, let’s look at the if-else conditional statement in Python:
points = 21.5
if points >= 20:
print('You are an all-star player')
else:
print('Good job, to be an all-star player!')
We can also use elif for more detailed conditional checks:
points = 21.5
if points >= 30:
print('You are an MVP player')
elif 25 <= points < 30:
print('You are a quasi MVP player')
elif 20 <= points < 30:
print('You are an all-star player')
else:
print('Good job, to be an all-star player!')
Python’s loop statements are consistent with other languages, and I will not elaborate further. I will demonstrate Python’s looping capabilities using the common Gaussian summation with for and while loops.
For loop:
sum = 0
for x in range(101):
sum = sum + x
print(sum)
5050
While loop:
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print(sum)
5050
From the examples above, you can see that Python’s data types and some programming fundamentals are quite simple, which is also related to Python’s overall language style, making it easy to read even for outsiders.
Good news!
Beginner Learning Vision Knowledge Planet
is now open to the public👇👇👇
Download 1: OpenCV-Contrib Extension Module Chinese Tutorial
Reply: Extension Module Chinese Tutorial in the backend of the “Beginner Learning Vision” public account to download the first Chinese version of the OpenCV extension module tutorial, covering installation of extension modules, SFM algorithms, stereo vision, object tracking, biological vision, super-resolution processing, and more than twenty chapters of content.
Download 2: Python Vision Practical Project 52 Lectures
Reply: Python Vision Practical Project in the backend of the “Beginner Learning Vision” public account to download 31 visual practical projects, including image segmentation, mask detection, lane line detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, and face recognition, to assist in quickly learning computer vision.
Download 3: OpenCV Practical Project 20 Lectures
Reply: OpenCV Practical Project 20 Lectures in the backend of the “Beginner Learning Vision” public account to download 20 practical projects based on OpenCV for advanced learning.
Group Chat
Welcome to join the reader group of the public account to exchange ideas with peers. Currently, we have WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (these will be gradually subdivided). Please scan the WeChat ID below to join the group, and note: “Nickname + School/Company + Research Direction”, for example: “Zhang San + Shanghai Jiao Tong University + Vision SLAM”. Please follow the format for notes; otherwise, you will not be approved. After successfully adding, you will be invited into relevant WeChat groups based on your research direction. Please do not send advertisements in the group; otherwise, you will be removed. Thank you for your understanding~