Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
UPS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IPDP
UPS
Commits
43baae47
Commit
43baae47
authored
3 years ago
by
vand
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
2fb974ba
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
module4_looping.py
+61
-0
61 additions, 0 deletions
module4_looping.py
with
61 additions
and
0 deletions
module4_looping.py
0 → 100644
+
61
−
0
View file @
43baae47
#%% example -- can't change iterator during for loop, formatting display
for
i
in
range
(
10
):
print
(
f
'
i is
{
i
}
, trying to change it to
{
3
*
i
}
'
)
i
=
3
*
i
# don't do this (trying to change an iterator in a for loop)
#%% example -- find largest number divisible with 7 and smaller than 100
i
=
1
while
i
*
7
<
100
:
i
=
i
+
1
print
(
7
*
i
)
# this will be larger than 100 -- so condition from while does not hold
#%% example -- vectorization
import
numpy
as
np
test
=
np
.
array
([
3
,
4
,
8
,
6
,
5
,
8
,
3
,
4
,
7
,
8
,
8
,
9
,
4
,
1
,
3
,
4
,
6
,
7
,
9
,
5
,
3
,
5
,
7
,
2
,
5
,
3
])
## I want to count how many times does 8 appear in test
# One way of achieving this
count
=
0
for
t
in
test
:
if
t
==
8
:
count
+=
1
# A better way of achieving this
count
=
(
test
==
8
).
sum
()
#%% I want to count how many times does each digit appear in test
# One way of achieving this
hist
=
np
.
zeros
(
10
,
dtype
=
'
int
'
)
for
i
in
range
(
len
(
hist
)):
hist
[
i
]
=
(
test
==
i
).
sum
()
# Another way of achieving this
hist
=
np
.
zeros
(
10
,
dtype
=
'
int
'
)
for
t
in
test
:
hist
[
t
]
+=
hist
[
t
]
#%% Avoid using break for better readability, if possible
# Without break: readable code, easly to see when while loop stops
i
=
0
while
i
<
101
:
i
=
i
+
1
print
(
i
,
end
=
'
'
)
# With break: may be difficult to see when loop stops (especially in loops spanning many lines)
i
=
0
while
True
:
i
=
i
+
1
print
(
i
,
end
=
'
'
)
if
i
>
100
:
break
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment