FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/conditionals/if_example.py at main · thomasWeise/programmingWithPythonCode · GitHub
thomasWeise
programmingWithPythonCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
conditionals
/
if_example.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
20 lines (16 loc) · 807 Bytes
Breadcrumbs
programmingWithPythonCode
/
conditionals
/
if_example.py
Copy path
File metadata and controls
20 lines (16 loc) · 807 Bytes
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
"""
An example of using the `if` statement.
A year is a leap year if it is divisible by 4 but not divisible by 100
or if it is divisible by 400.
We can use the modulo operator `%` to check this.
`a % 100` will be `0` if and only `a` is a multiple of `100`.
The Boolean statements can be combined with `and` and `or` and grouped
using parentheses.
"""
year
:
int
=
2024
# The year 2024 should be a leap year.
if
(((
year
%
4
)
==
0
)
and
((
year
%
100
)
!=
0
))
or
((
year
%
400
)
==
0
):
print
(
f"
{
year
}
is a leap year."
)
# This line will be executed.
year
=
1900
# The year 1900 is not a leap year.
if
(((
year
%
4
)
==
0
)
and
((
year
%
100
)
!=
0
))
or
((
year
%
400
)
==
0
):
print
(
f"
{
year
}
is a leap year."
)
# This line is never reached.
print
(
"End of year checking."
)
# This text is always printed.
Back
|
FazBrowse Home
|
New Git URL