![]() |
|
[Tut] Python Tuple to Integer - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Tuple to Integer (/thread-96015.html) |
[Tut] Python Tuple to Integer - xSicKxBot - 07-03-2020 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. Code: 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: https://trinket.io/embed/python/bf5faba68b 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 Code: sum()The syntax is Code: sum(iterable, start=0)Argument | Description Code: iterableSum over all elements in the Code: iterableExample: Code: sum([1, 2, 3])Code: 1+2+3=6Code: start(Optional.) The default start value is 0. If you define another start value, the sum of all values in the Code: iterableExample: Code: sum([1, 2, 3], 9)Code: 9+1+2+3=15Here’s how you can use the Code: sum()Code: # Method 1: sum()
t = (1, 2, 3)
i = sum(t)
print(i)
# 6In this case, it calculates 1+2+3=6. You can learn more about the Code: sum()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 Code: [expression + context]
You can use it in combination with the Code: sum()Code: # Method 2: str() + list comprehension + join()
t = (1, 2, 3)
i = ''.join(str(x) for x in t)
print(int(i))
# 123Well, to be frank, we didn’t even use list comprehension here—the correct term for Code: str(x) for x in tIf 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 Code: str()Code: map()Then, you can joinall 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. Code: # Method 3: str() + map() + join()
t = (1, 2, 3)
i = ''.join(map(str, t))
print(i)
# 123There are many details to the Code: string.join()The Code: string.join(iterable)Code: iterableCode: stringCode: '-'.join(['hello', 'world'])Code: '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: Code: # Method 4: multiple assignments
t = (1, 2, 3)
a, b, c = t
print(a)
print(b)
print(c) '''
1
2
3 '''Variables Code: aCode: bCode: cWhere 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! |