Skip to content

Commit e2c1e88

Browse files
authored
Merge pull request #11 from arpitHub/main
Merge New Post
2 parents 086db49 + 45a4849 commit e2c1e88

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: post
3+
title: Uses of Underscore in Python
4+
subtitle: Underscore examples in Python
5+
cover-img: /assets/img/path.jpg
6+
thumbnail-img: /assets/img/thumb.png
7+
share-img: /assets/img/path.jpg
8+
gh-repo: arpithub/arpithub.github.io
9+
gh-badge: [star, fork, follow]
10+
tags: [python,python_tips]
11+
comments: true
12+
---
13+
14+
We all have seen underscode symbol in Python. One of the most common uses of the underscore is as a placeholder variable. When iterating over a sequence or generating a range of values, there are often situations where the loop variable is not needed. In such cases, Python programmers opt to use _ as a concise way to indicate that the value is irrelevant to the current context.
15+
16+
```python
17+
for _ in range(10):
18+
print("Doh!")
19+
```
20+
21+
Here, _ acts as a placeholder for the loop variable, highlighting the intent to iterate a specific number of times without the need to reference the loop index.
22+
23+
Moreover, _ serves as a convention for denoting unused variables. In scenarios where only one value from a function call or tuple unpacking is required, Python programmers often assign the unused value to _, signaling to others that the value is intentionally disregarded.
24+
25+
```python
26+
first_name, _ = get_name()
27+
```
28+
This will return multiple parameters but we ignore other parameters except `first_name`.
29+
30+
```python
31+
>>> 1 + 2 + 3
32+
6
33+
>>> _ * 5
34+
30
35+
```
36+
Here underscode saves the value of last expression ie. `1+2+3=5`
37+
38+
In short, Underscore `_` can be used as a placeholder, unused variable indicator or temporary result storage. It makes our code readable and expressive. Happy Code Writing!

0 commit comments

Comments
 (0)