Machine Learning News Hubb
Advertisement Banner
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
Machine Learning News Hubb
No Result
View All Result
Home Artificial Intelligence

4 Basic Commands When Working with Python Tuples | by Niklas Lang | Sep, 2022

admin by admin
September 10, 2022
in Artificial Intelligence


Making you understand the characteristics of Python Tuples and how you deal with them

Photo by Washington Oliveira 🇧🇷 on Unsplash

The Python tuple is used to store multiple values in a variable. It is one of four data structures that are pre-installed in Python. In addition to the tuple, these also include the dictionary, the set, and the list.

The tuple is ordered and cannot be changed. On the one hand, this means that the elements have a specific and constant order. Due to the fixed order, the tuple also allows duplicates. On the other hand, the tuple cannot be changed after it has been defined. Thus, no elements can be deleted or added.

Python has a total of four different data types, which are already present in the basic installation and can therefore not be used only by installing a module, such as Panda’s DataFrames. These can be used for a wide variety of use cases and also form the basis for many other data types used in modules.

The four basic data types in Python are:

  • The list is an ordered collection of elements, which is changeable and can also contain duplicate elements.
  • The tuple is in effect a list, with the difference that it is no longer changeable. So no elements can be added or removed afterward.
  • The set does not allow duplicate entries. At the same time, the arrangement of the elements within the set is variable. The set itself can be changed, but the individual elements cannot be changed afterward.
  • Since Python version 3.7, a dictionary is an ordered collection of elements that can be changed. In the earlier versions, the dictionary is unordered.

We can create a Python tuple by defining the elements in round brackets and separating them with commas. Elements with different data types can be stored in a tuple without any problems.

# Define a tuple
tuple_1 = ('first element', 5, 'third Element', True)

Due to the order of a Tuple, we can resort to indices to retrieve individual elements from the tuple. It should be noted that the counting of elements starts at 0. If we want to retrieve a value from the end, on the other hand, we start counting at 1.

# Define a tuple
tuple_1 = ('first element', 5, 'third Element', True)
# Print first element of the tuple
print(tuple_1[0])
# Print last element of the tuple
print(tuple_1[-1])
Out:
'first element'
True

If we don’t know the index of a certain element yet, we can ask for it with the method “index”. Since the order within the Python Tuple does not change, this value also remains.

# Define a tuple
tuple_1 = ('first element', 5, 'third Element', True)
# Get index of an element
print(tuple_1.index(5))
Out:
1

As we have already learned, Tuples are actually immutable. That is, once we have defined a Tuple, it is no longer possible to add or delete elements.

To be able to change Tuples anyway, we use a little trick. We first convert the Tuple into a Python list. Since this is changeable, we can simply add or remove elements here. Then we convert the list back to a Tuple. This way we have indirectly changed the elements of the Python Tuple.

# Define a tuple
tuple_1 = ('first element', 5, 'third Element', True)
# Transform to a list
list_1 = list(tuple_1)
# Add item
list_1.append('new_element')
# Remove item
list_1.remove('third Element')
# Transform back to a tuple
tuple_1 = tuple(list_1)
print(tuple_1)
Out:
('first element', 5, True, 'new_element')

If we want to merge two or more Tuples, we can simply use the “+” operator. The first named Tuple is accordingly in the order before the second named Tuple.

# Define two tuples
tuple_1 = ('first element', 2, True)
tuple_2 = (1, 4, 6)
# Add them together
tuple_3 = tuple_1 + tuple_2
print(tuple_3)
Out:
('first element', 2, True, 1, 4, 6)



Source link

Previous Post

Mux + CLIPr: Simulcasting for deeper engagement | by Mack Reed | The Moment | Sep, 2022

Next Post

Third Place Analysis and Implementation (Design, Data, Artificial Intelligence) | by NJ Namju Lee | Sep, 2022

Next Post

Third Place Analysis and Implementation (Design, Data, Artificial Intelligence) | by NJ Namju Lee | Sep, 2022

SQL Window Functions Interview Questions | by Nathan Rosidi | Sep, 2022

HOG Feature Descriptor. In computer vision there are many… | by Dahi Nemutlu | Sep, 2022

Related Post

Artificial Intelligence

Exploring TensorFlow Model Prediction Issues | by Adam Brownell | Feb, 2023

by admin
February 2, 2023
Machine Learning

Different Loss Functions used in Regression | by Iqra Bismi | Feb, 2023

by admin
February 2, 2023
Machine Learning

How to organize bills? – 3 ways to track bills

by admin
February 2, 2023
Artificial Intelligence

How to decide between Amazon Rekognition image and video API for video moderation

by admin
February 2, 2023
Artificial Intelligence

The Future of AI: GPT-3 vs GPT-4: A Comparative Analysis | by Mohd Saqib | Jan, 2023

by admin
February 2, 2023
Deep Learning

6 Ways To Streamline Tech Hiring With A Recruitment Automation Platform

by admin
February 2, 2023

© 2023 Machine Learning News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy and Terms & Conditions.

Navigate Site

  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

Newsletter Sign Up.

No Result
View All Result
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.