Skip to content

Commit b435ab6

Browse files
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

Lines changed: 34 additions & 0 deletions
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 '!'.**
Lines changed: 10 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('firstname', 'sname', sep='*', end='*') #change this line
Lines changed: 20 additions & 0 deletions
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**
Lines changed: 7 additions & 0 deletions
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"')
Lines changed: 4 additions & 0 deletions
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)
Lines changed: 30 additions & 0 deletions
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'.**
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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'")
Lines changed: 8 additions & 0 deletions
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')

0 commit comments

Comments
 (0)