Skip to content
Snippets Groups Projects
Commit ee5037bb authored by Sarah Bradford's avatar Sarah Bradford
Browse files

Adding Loops, Numpy Sympy, Whitespace

parent 8b95fb0b
No related branches found
No related tags found
1 merge request!7Sarah branch
%% Cell type:markdown id:b64fb56b tags:
# Loops
%% Cell type:markdown id:a4435deb tags:
# Short/Long Title: Loops
%% Cell type:markdown id:eb8f94bb tags:
# Description:
%% Cell type:markdown id:4c94faf5 tags:
Loops in Python are used to execute a series of code or statements back to back until a specific condition is true or satisfied in a list, tuple, or set. Once the condition is recognized as false, the upcoming line of code is stopped. There are a few different types of loops.
%% Cell type:markdown id:cec184aa tags:
# Types of Loops
%% Cell type:markdown id:f14394ce tags:
A while loop is used to evaluate a condition before processing a body of code inside of a loop. If a statement is false, the body of the code will not be executed. While, if it is found true only the body of the loop is executed.
A do-while loop or exit-controlled loop allows for a condition to always be executed after the body of a loop.
%% Cell type:markdown id:39abaad6 tags:
# Learning Materials
%% Cell type:code id:9ecc2028 tags:
``` python
from IPython.display import YouTubeVideo
YouTubeVideo("dHANJ4l6fwA",width=640,height=360, cc_load_policy=True)
```
%% Output
<IPython.lib.display.YouTubeVideo at 0x7fe0b87523d0>
%% Cell type:code id:afdfe29c tags:
``` python
from IPython.display import YouTubeVideo
YouTubeVideo("OnDr4J2UXSA",width=640,height=360, cc_load_policy=True)
```
%% Output
<IPython.lib.display.YouTubeVideo at 0x7fe0b876efa0>
%% Cell type:markdown id:4d4a0e2e tags:
# Self-Assessment
%% Cell type:markdown id:d56f121b tags:
Create a function to print the first ten even numbers.
%% Cell type:code id:c55076ee tags:
``` python
#Put Your Answer Here
for i in range(2,22,2):
print(i)
```
%% Output
2
4
6
8
10
12
14
16
18
20
%% Cell type:markdown id:6abad683 tags:
Define the function prime(num) : return false
%% Cell type:code id:eb447093 tags:
``` python
#Put Your Answer Here
num=int(input("Enter any number"))
answer=false
if num==1 or num==0:
answer=true
for i in range(2,num):
if num%i==0:
answer=true
if answer==true:
print("Number is not prime")
else:
print("Number is prime")
Assert(prime(11))
Assert(prime(3))
```
%% Output
Enter any number1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [5], in <cell line: 3>()
1 #Put Your Answer Here
2 num=int(input("Enter any number"))
----> 3 answer=false
4 if num==1 or num==0:
5 answer=true
NameError: name 'false' is not defined
%% Cell type:code id:1474fb12 tags:
``` python
```
This diff is collapsed.
%% Cell type:markdown id:950c4fef tags:
# Whitespace Sensitivity
%% Cell type:markdown id:019e60ef tags:
Short Title: Indentation
%% Cell type:markdown id:c61cba51 tags:
# Description
%% Cell type:markdown id:55abe61f tags:
Whitespaces or indedentation act as the spaces at the very start or inside of a code for enhanced readibility in Python. It is used to designate a block of code inside of a program. Any code without indentation or white space is attached to a specific source file. Whitespaces tend to be sensitive due to their ability to completely effect the possibilities of an output of a code or interpretation of a statement ultimately causing an error to pop up.
It is important to understand, whitespace must always be consistant throughout code. So if a programmer starts with 5 spaces, 5 space must be used throughout the remainder of the block of code or else a syntax error will not allow the code to be ran.
%% Cell type:markdown id:f8796bb7 tags:
# Examples
%% Cell type:markdown id:410f3f67 tags:
'' - space
/t - tab
%% Cell type:markdown id:880c1d84 tags:
# Learning Materials
%% Cell type:code id:dfad44a7 tags:
``` python
from IPython.display import YouTubeVideo
YouTubeVideo("vgefSv5k-o4",width=640,height=360, cc_load_policy=True)
```
%% Output
<IPython.lib.display.YouTubeVideo at 0x7ff3796ec7c0>
%% Cell type:markdown id:50fdb729 tags:
10 minutes
%% Cell type:markdown id:4ae1c549 tags:
# Self-Assessment
%% Cell type:markdown id:a2e5459e tags:
Create a code that tests whitespace values.
%% Cell type:code id:80ee2cd9 tags:
``` python
#Put your answer here
# import string
import string
# An input string.
Sentence = "What time does lunch begin at Shaw?"
for i in Sentence:
# determines if whitespace is found in code
if i in string.whitespace:
# Prints the number of whitespace values and its characters
print("printable Value is: " + i)
```
%% Output
printable Value is:
printable Value is:
printable Value is:
printable Value is:
printable Value is:
printable Value is:
%% Cell type:code id:351205d1 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment