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
671d1722
Commit
671d1722
authored
3 years ago
by
vand
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
010e48ec
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
module3_wrapup.py
+86
-0
86 additions, 0 deletions
module3_wrapup.py
with
86 additions
and
0 deletions
module3_wrapup.py
0 → 100644
+
86
−
0
View file @
671d1722
## module 3, example - not needing selection statement
x
=
5
# I want to make sure that x is not smaller than 0
# One way of achieving this:
if
x
<
0
:
x
=
0
# A better way of achieving the same:
x
=
max
(
x
,
0
)
#%% module 3, example - short-circuit operators
name
=
'
Vedrana
'
# I want to check if the name starts with 'Ved'. But what if name has less than
# 3 letters? I need to make sure that name[:3] is not evaluated if name is too
# short. I add the first condition and the second condition will not be
# evaluated if the first is false.
if
len
(
name
)
>
2
and
name
[:
3
]
==
'
Ved
'
:
print
(
'
Cool
'
)
#%% module 3, example -- an extra condition
x
=
4
# I want to check whethr x is bigger than 5
# A bad way of achieving this:
if
x
>
5
:
print
(
'
Bigger than 5
'
)
elif
x
<=
5
:
print
(
'
Smaller or equal 5
'
)
# A better way of achieving this:
if
x
>
5
:
print
(
'
Bigger than 5
'
)
else
:
print
(
'
Smaller or equal 5
'
)
#%% module 3, example -- nothing to fall to
# Make sure that return value is assigned in the function call
def
give_verdict
(
x
):
x
=
''
if
x
>
0
:
verdict
=
'
Bigger than 0
'
elif
x
<
0
:
verdict
=
'
Smaller than 0
'
return
verdict
print
(
give_verdict
(
0
))
#%% module 3, example -- using boolean flags
is_rgb
=
False
if
is_rgb
:
# dont write "if is_rgb==True"
print
(
'
special treatment for color images
'
)
else
:
print
(
'
no special treatment
'
)
#%% module 3, example -- debugging, line by line
x
=
41.7
if
x
>
137
/
4
:
x
=
2
*
x
+
154
/
18
elif
x
<
117
/
4
:
x
=
x
+
154
/
18
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