Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Tuple to Integer

#1
Python Tuple to Integer

You have a tuple of integers—but you want a single integer. What can you do?

Problem: Given a tuple of values.

t = (1, 2, 3)

Your goal is to convert it to a single integer value.

There are multiple ways of accomplishing this (dependent on what exactly you want to do). Let’s get a quick overview in our interactive Python shell:

Exercise: Modify method 2 to calculate the average and round to the next integer!

Let’s dive into each of the method.

Method 1: sum()


The first way of converting a tuple to an integer, simply sum up all values. The sum() function is built-in in Python and you can use it on any iterable:

The syntax is sum(iterable, start=0):


Argument Description
iterable Sum over all elements in the iterable. This can be a list, a tuple, a set, or any other data structure that allows you to iterate over the elements.
Example: sum([1, 2, 3]) returns 1+2+3=6.
start (Optional.) The default start value is 0. If you define another start value, the sum of all values in the iterable will be added to this start value.
Example: sum([1, 2, 3], 9) returns 9+1+2+3=15.



Here’s how you can use the sum() function to sum over all values in an iterable (such as a tuple):

# Method 1: sum()
t = (1, 2, 3)
i = sum(t)
print(i)
# 6

In this case, it calculates 1+2+3=6. You can learn more about the sum() function on this Finxter blog article.

But what if you want to use all tuple values as digits of a larger integer value?

Method 2: str() + list comprehension + join()


List comprehension is a compact way of creating lists. The simple formula is [expression + context].

  • Expression: What to do with each list element?
  • Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can use it in combination with the sum() function to calculate the integer 123 from the tuple (1, 2, 3)—by using the tuple values as digits of the larger integer.

# Method 2: str() + list comprehension + join()
t = (1, 2, 3)
i = ''.join(str(x) for x in t)
print(int(i))
# 123

Well, to be frank, we didn’t even use list comprehension here—the correct term for str(x) for x in t is “generator expression”. The difference to list comprehension is that it creates a generator instead of a list.

If you like functional programming, you may like the following method:

Method 3: str() + map() + join()


The map() function creates a new iterable from an iterable by applying a function to each element of the original iterable:



You can pass the str() function into the map() function to convert each tuple element to a string.

Then, you can join all strings together to a big string. After converting the big string to an integer, you’ve successfully merged all tuple integers to a big integer value.

# Method 3: str() + map() + join()
t = (1, 2, 3)
i = ''.join(map(str, t))
print(i)
# 123

There are many details to the string.join() method. You can read the detailed tutorial on the Finxter blog. Here’s the short version:

The string.join(iterable) method concatenates all the string elements in the iterable (such as a list, string, or tuple) and returns the result as a new string. The string on which you call it is the delimiter string—and it separates the individual elements. For example, '-'.join(['hello', 'world']) returns the joined string 'hello-world'.

Method 4: Multiple Assignments


If you simply want to get multiple integers by assigning the individual tuple values to integer variables, just use the multiple assignment feature:

# Method 4: multiple assignments
t = (1, 2, 3)
a, b, c = t
print(a)
print(b)
print© '''
1
2
3 '''

Variables a, b, and c have the values 1, 2, and 3, respectively.

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2020/07/...o-integer/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] PEP 8: Hanging Indentation and Closing Brackets in Python xSicKxBot 0 3 4 hours ago
Last Post: xSicKxBot
  [Tut] How to Create a Database Table with pyodbc in Python xSicKxBot 0 4 Yesterday, 08:37 AM
Last Post: xSicKxBot
  [Tut] Python One Line Return if xSicKxBot 0 3 08-01-2020, 07:08 PM
Last Post: xSicKxBot
  [Tut] Top 5 Python Freelancer Jobs to Earn $51 per Hour on Upwork or Fiverr xSicKxBot 0 6 07-31-2020, 06:46 PM
Last Post: xSicKxBot
  [Tut] Python One Line Reverse Shell xSicKxBot 0 7 07-30-2020, 04:50 PM
Last Post: xSicKxBot
  [Tut] Python One Line Exception Handling xSicKxBot 0 6 07-29-2020, 04:10 PM
Last Post: xSicKxBot
  [Tut] What is __init__ in Python? xSicKxBot 0 12 07-27-2020, 06:49 PM
Last Post: xSicKxBot
  [Tut] Python One Line Quine xSicKxBot 0 12 07-26-2020, 05:00 PM
Last Post: xSicKxBot
  [Tut] Python One Line While Loop [A Simple Tutorial] xSicKxBot 0 17 07-25-2020, 10:34 AM
Last Post: xSicKxBot
  [Tut] Python One Line For Loop Lambda xSicKxBot 0 14 07-24-2020, 09:48 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Upcoming Events

[-]
Latest Threads
[Tut] PEP 8: Hanging Indentation and Clo...
Last Post: xSicKxBot
Today 09:51 AM
» Replies: 0
» Views: 3
(Indie Deal) Bundles & Sales Round-up
Last Post: xSicKxBot
Today 09:50 AM
» Replies: 0
» Views: 2
Microsoft - New Microsoft Store experien...
Last Post: xSicKxBot
Today 09:50 AM
» Replies: 0
» Views: 1
Fedora - Backup and restore Toolboxes
Last Post: xSicKxBot
Today 09:49 AM
» Replies: 0
» Views: 1
News - Zelda, Mario And Animal Crossing ...
Last Post: xSicKxBot
Today 09:49 AM
» Replies: 0
» Views: 1
News - Today Is Masahiro Sakurai’s 50th ...
Last Post: xSicKxBot
Today 09:49 AM
» Replies: 0
» Views: 1
News - Marvel's Avengers Will Add Spider...
Last Post: xSicKxBot
Today 09:49 AM
» Replies: 0
» Views: 1
mens leather wallet
Last Post: Leila Elinor
Today 04:05 AM
» Replies: 0
» Views: 4
panama hut
Last Post: Leila Elinor
Today 04:00 AM
» Replies: 0
» Views: 3
nike sneakers for men
Last Post: Leila Elinor
Today 03:48 AM
» Replies: 0
» Views: 5

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2019