Start with Python
This commit is contained in:
76
1- python/3-conditionals-and-for-loops/README.md
Normal file
76
1- python/3-conditionals-and-for-loops/README.md
Normal file
@ -0,0 +1,76 @@
|
||||
Conditionals and For Loops
|
||||
==========================
|
||||
|
||||
Get ready for FizzBuzz, again!
|
||||
|
||||
#### For Loop
|
||||
|
||||
For loop syntax in Python is slightly different.
|
||||
The 'for in' pattern is still available in Python:
|
||||
```
|
||||
>>> for thing in list:
|
||||
... print(thing)
|
||||
```
|
||||
Python also has what's known as a range:
|
||||
```
|
||||
>>> lst = ['a', 'b', 'c']
|
||||
>>> for i in range(len(lst)):
|
||||
... print(i)
|
||||
0
|
||||
1
|
||||
2
|
||||
```
|
||||
If we wanted to print the element in the list, rather than the index, we could do it like this:
|
||||
```
|
||||
>>> lst = ['a', 'b', 'c']
|
||||
>>> for i in range(len(lst)):
|
||||
... print(lst[i])
|
||||
a
|
||||
b
|
||||
c
|
||||
```
|
||||
With range, you have the option to choose the start and stop points, as well as increment.
|
||||
```
|
||||
>>> for i in range(2, 10, 2):
|
||||
... print(i)
|
||||
2
|
||||
4
|
||||
6
|
||||
8
|
||||
```
|
||||
As you can see, the end point is not included as part of the range.
|
||||
|
||||
Fire up your Python interpreter and try it out.
|
||||
|
||||
#### Conditionals - Switch
|
||||
|
||||
Switch statements are basically the same in Python. You can write if statements in two ways:
|
||||
```
|
||||
if(some condition):
|
||||
do something
|
||||
```
|
||||
or
|
||||
```
|
||||
if some condition:
|
||||
do something
|
||||
```
|
||||
for else clauses, it's also pretty similar.
|
||||
Here is an example:
|
||||
```
|
||||
if condition:
|
||||
do something
|
||||
elif another condition:
|
||||
do something else
|
||||
else:
|
||||
catch all of the other possibilities
|
||||
```
|
||||
|
||||
#### FizzBuzz
|
||||
|
||||
Time for FizzBuzz in Python.
|
||||
|
||||
Write code that does the following:
|
||||
* if i is divisible by 3, print "Fizz"
|
||||
* if i is divisible by 5, print "Buzz"
|
||||
* if i is divisible by 3 & 5, print "FizzBuzz"
|
||||
* if i is not divisble by 3 or 5, print i
|
11
1- python/3-conditionals-and-for-loops/fizzbuzz.py
Normal file
11
1- python/3-conditionals-and-for-loops/fizzbuzz.py
Normal file
@ -0,0 +1,11 @@
|
||||
def fizzbizz(num):
|
||||
pass
|
||||
|
||||
|
||||
def assertion(actual, expected):
|
||||
print(str(actual) + " == " + str(expected) + " : " + str(actual==expected))
|
||||
|
||||
assertion(fizzbuzz(33), "Fizz")
|
||||
assertion(fizzbuzz(20), "Buzz")
|
||||
assertion(fizzbuzz(30), "FizzBuzz")
|
||||
assertion(fizzbuzz(32), 32)
|
Reference in New Issue
Block a user