FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/loops/for_loop_sequence.py at main · mlecjm/programmingWithPythonCode · GitHub
mlecjm
programmingWithPythonCode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
loops
/
for_loop_sequence.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (20 loc) · 1.26 KB
Breadcrumbs
programmingWithPythonCode
/
loops
/
for_loop_sequence.py
Copy path
File metadata and controls
26 lines (20 loc) · 1.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Iterate over several different containers with `for` loops."""
txt
:
list
[
str
]
=
[]
# We will collect the output text in this list.
lst
:
list
[
int
]
=
[
1
,
2
,
3
,
50
]
# Create a list with 4 integers.
for
i
in
lst
:
# i takes on the values 1, 2, 3, and 50.
txt
.
append
(
f"i=
{
i
}
"
)
# We store "i=1", "i=2", "i=3", and "i=50"
tp
:
tuple
[
float
, ...]
=
(
7.6
,
9.4
,
8.1
)
# Create a tuple with 3 floats.
for
f
in
tp
:
# i takes on the values 7.6, 9.4, and 8.1.
txt
.
append
(
f"f=
{
f
}
"
)
# We store "f=7.6", "f=9.4", and "f=8.1"
st
:
set
[
str
]
=
{
"u"
,
"v"
,
"w"
}
# Create a set with 3 strings.
for
s
in
st
:
# s takes on the values "u", "v", and "w" (unordered!).
txt
.
append
(
f"s=
{
s
!r
}
"
)
# We store "s='u'", "s='v'", and "s='w'".
dc
:
dict
[
float
,
bool
]
=
{
1.1
:
True
,
2.5
:
False
}
# Create a dictionary.
for
k
in
dc
:
# Iterate over the keys in the dictionary == 1.1 and 2.5.
txt
.
append
(
f"
{
k
=
}
"
)
# We store "k=1.1" and "k=2.5".
for
v
in
dc
.
values
():
# Iterate over the values in the dictionary.
txt
.
append
(
f"
{
v
=
}
"
)
# We store "v=True" and "v=False".
for
k
,
v
in
dc
.
items
():
# Iterate over the key-value combinations.
txt
.
append
(
f"
{
k
}
:
{
v
}
"
)
# Store "1.1: True" and "2.5: False"
# Merge text into single string with separator ", " and print it.
print
(
", "
.
join
(
txt
))
Back
|
FazBrowse Home
|
New Git URL