Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PythonIntro
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
Service Desk
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PythonDataScienceMatSci
PythonIntro
Commits
54013784
Commit
54013784
authored
Nov 7, 2022
by
Ulrich Kerzel
Browse files
Options
Downloads
Patches
Plain Diff
for and while loops
parent
b4c47084
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pythonintro/Solutions_Python.ipynb
+98
-0
98 additions, 0 deletions
pythonintro/Solutions_Python.ipynb
with
98 additions
and
0 deletions
pythonintro/Solutions_Python.ipynb
0 → 100644
+
98
−
0
View file @
54013784
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"run\n"
]
}
],
"source": [
"##\n",
"## print the word 'run' from the string 'nurse'\n",
"##\n",
"my_word = 'nurse'\n",
"print(my_word[2::-1])\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Fibonacci numbers are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n"
]
}
],
"source": [
"##\n",
"## Fibonacci Numbers --- For Loop\n",
"##\n",
"\n",
"Fibonacci = [0, 1]\n",
"for i in range(2,10,1):\n",
" Fib = Fibonacci[i-1] + Fibonacci[i-2]\n",
" Fibonacci.append(Fib)\n",
"\n",
"print('The Fibonacci numbers are: {}'.format(Fibonacci))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##\n",
"## Fibonacci Numbers --- While Loop\n",
"##\n",
"\n",
"Fibonacci = [0, 1]\n",
"\n",
"while len(Fibonacci) < 10:\n",
" Fib = Fibonacci[-1] + Fibonacci[-2]\n",
" Fibonacci.append(Fib)\n",
"\n",
"print('The Fibonacci numbers are: {}'.format(Fibonacci))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('pythonintro-A08DqnGu-py3.10')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "eb3a944512c48027e49906d9e47d21cfb9b2c1ddd33f9b79c72df4b5e3a553dc"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
```
python
##
## print the word 'run' from the string 'nurse'
##
my_word
=
'
nurse
'
print
(
my_word
[
2
::
-
1
])
```
%% Output
run
%% Cell type:code id: tags:
```
python
##
## Fibonacci Numbers --- For Loop
##
Fibonacci
=
[
0
,
1
]
for
i
in
range
(
2
,
10
,
1
):
Fib
=
Fibonacci
[
i
-
1
]
+
Fibonacci
[
i
-
2
]
Fibonacci
.
append
(
Fib
)
print
(
'
The Fibonacci numbers are: {}
'
.
format
(
Fibonacci
))
```
%% Output
The Fibonacci numbers are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
%% Cell type:code id: tags:
```
python
##
## Fibonacci Numbers --- While Loop
##
Fibonacci
=
[
0
,
1
]
while
len
(
Fibonacci
)
<
10
:
Fib
=
Fibonacci
[
-
1
]
+
Fibonacci
[
-
2
]
Fibonacci
.
append
(
Fib
)
print
(
'
The Fibonacci numbers are: {}
'
.
format
(
Fibonacci
))
```
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