Skip to content

Commit b435ab6

Browse files
authoredJul 31, 2017
Merge pull request freeCodeCamp#37 from Jamaurz/master
Chapter 1 python challenges
2 parents 841a8f2 + 789601b commit b435ab6

File tree

14 files changed

+192
-0
lines changed

14 files changed

+192
-0
lines changed
 

‎challenges/1.1.Print/lesson.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Print
2+
- http://www.python-course.eu/python3_print.php
3+
The print() function is used to present the output of a program.
4+
```
5+
>>> print(“Hello World”)
6+
Hello World
7+
```
8+
It can have several parameters.
9+
```
10+
>>> print(‘Hello’, 12, ‘98’)
11+
Hello 12 98
12+
```
13+
The arguments of the print function:
14+
sep=’’
15+
Sep is a separator, it is used to divide parameters.
16+
```
17+
>>> print(‘Hello’, “World’, sep=’-‘)
18+
Hello-World
19+
```
20+
We can assign an string to the keyword parameter "end". This string will be used for ending the output of the values of a print call.
21+
end=’’
22+
```
23+
>>> print(‘Hello’, ‘World’, end=’!’)
24+
Hello World?
25+
```
26+
By redefining the keyword parameter "file" we can send the output into a different stream file.
27+
file=’’
28+
```
29+
>>> fh = open("data.txt","w")
30+
>> print("no", file=fh)
31+
>>> fh.close()
32+
```
33+
**_Instructions:_**
34+
**Put your name and sname to appropriate places, change '*' to parameters of separator '_' and in the end of the line '!'.**

‎challenges/1.1.Print/lesson_tests.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
3+
4+
class PrintTests(unittest.TestCase):
5+
def test_main(self):
6+
f = open('main.py')
7+
lines = str(f.readlines())
8+
f.close()
9+
self.assertRegex(lines, "sep='_'", msg="_ mising")
10+
self.assertRegex(lines, "end='!'", msg="! mising")

‎challenges/1.1.Print/main.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('firstname', 'sname', sep='*', end='*') #change this line
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Escape Character
2+
The escape character is "\\" and this character is invoked an alternative interpretation in a character sequence.
3+
- https://docs.python.org/2.0/ref/strings.html
4+
```
5+
>>> print('\\')
6+
\
7+
>>> print('\'')
8+
'
9+
>>> print('Helllo\nWorld')
10+
Hello
11+
World
12+
```
13+
We can use escape character to print hex value Unicode character.
14+
```
15+
>>> print('\uxxxx')
16+
Л
17+
```
18+
19+
**_Instructions:_**
20+
**Use escape character "new line" in the string_escape**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import unittest
2+
from main import *
3+
4+
5+
class EscapeTests(unittest.TestCase):
6+
def test_main(self):
7+
self.assertRegex(repr(string_escape), r'\\n', msg='the string must contain "new line"')
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
string_escape = 'Hello, this is escape string' # change this line
2+
3+
# change the string_escape, the string must contain "new line"
4+
print(string_escape)

‎challenges/6.1.Conditionals/lesson.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## if elif else
2+
3+
- https://docs.python.org/3/tutorial/controlflow.html
4+
5+
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. In Python zero and null is assumed as FALSE value.
6+
```
7+
if value == 'some':
8+
print('yes')
9+
```
10+
An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
11+
```
12+
if value == 'some':
13+
print('yes')
14+
else:
15+
print('no')
16+
```
17+
18+
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
19+
20+
```
21+
if value == 'some':
22+
print('yes')
23+
elif value = 'value':
24+
print('maybe')
25+
else:
26+
print('no')
27+
```
28+
29+
**_Instructions_**
30+
**Modify the value, so that program must print 'yes'.**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lesson_title": "Conditionals",
3+
"chapter_number": 6,
4+
"lesson_number": 1,
5+
"id":"5965e5fd60d67217f0ce232f",
6+
"repl": ""
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import unittest
2+
from main import *
3+
4+
class ConditionalsTests(unittest.TestCase):
5+
def test_main(self):
6+
self.assertIsInstance(value, str)
7+
self.assertIs(value, 'y', "program must print 'yes'")

‎challenges/6.1.Conditionals/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
value = 'some' #modify this line
2+
3+
if value == 'Y' or value == 'y':
4+
print('yes')
5+
elif value == 'N' or value == 'n':
6+
print('no')
7+
else:
8+
print('error')

‎challenges/7.1.Loops/lesson.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Loops (While)
2+
3+
- https://docs.python.org/3/tutorial/controlflow.html
4+
5+
A loop statement allows to execute a block of code multiple times. Python supplies two type of loops:
6+
* while
7+
* for
8+
9+
The syntax of a while loop is:
10+
11+
```
12+
while condition:
13+
statement
14+
```
15+
16+
While loops repeat a target statement as long as a given condition is true.
17+
18+
```
19+
count = 0
20+
while count < 9:
21+
print('Loop iteration: ', count)
22+
count = count + 1
23+
```
24+
25+
Python supports to have an else statement associated with a loop statement. The else statement is executed when the condition becomes false.
26+
27+
```
28+
count = 0
29+
while count < 9:
30+
print('Loop iteration ', count)
31+
count = count + 1
32+
else:
33+
print('loop end')
34+
```
35+
36+
**_Instructions_**
37+
**Modify the count, so that condition and switch_loop must become true.**
38+
**Use else statement, to mark the end of a program and switch switch_end to value true.**
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"lesson_title": "Loops",
3+
"chapter_number": 7,
4+
"lesson_number": 1,
5+
"id":"597e1a1a634a36abfebcda38",
6+
"repl": ""
7+
}

‎challenges/7.1.Loops/lesson_tests.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import unittest
2+
from main import *
3+
4+
class LoopsTests(unittest.TestCase):
5+
def test_main(self):
6+
self.assertTrue(switch_end)
7+
self.assertTrue(switch_loop)

‎challenges/7.1.Loops/main.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
count = 10 # Change this count
2+
switch_loop = False
3+
switch_end = False
4+
5+
while count < 9:
6+
print('step ', count)
7+
count = count + 1
8+
switch_loop = True
9+
# use else statements
10+
print('loop end')
11+
switch_end = True;
12+

0 commit comments

Comments
 (0)
Please sign in to comment.