Skip to content

New Excercises #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
289931d
Update if.md and loops.md
georgerahul24 Dec 30, 2020
6f1d882
Update if.md and loops.md
georgerahul24 Dec 30, 2020
9181d7a
Fixed Typo
georgerahul24 Jan 1, 2021
d5cf490
Removed unnecessary comments in the programmes
georgerahul24 Jan 1, 2021
848b0f3
Merge branch 'Akuli:master' into master
georgerahul24 Jul 1, 2021
33049e1
Update basics/loops.md
georgerahul24 Jul 13, 2021
a509a8e
Update basics/loops.md
georgerahul24 Jul 13, 2021
8fd60e4
Update basics/answers.md
georgerahul24 Jul 13, 2021
1eeda48
I have made the necessary changes as requested
georgerahul24 Jul 13, 2021
2ce1e7d
Update LICENSE
georgerahul24 Jul 13, 2021
7a93d2a
Update basics/loops.md
georgerahul24 Jul 13, 2021
39f94f5
Update basics/loops.md
georgerahul24 Jul 13, 2021
546f769
Update basics/handy-stuff-strings.md
georgerahul24 Jul 13, 2021
afafcdb
Update basics/answers.md
georgerahul24 Jul 13, 2021
9f35e3a
updated handy-stuff-strings.md
georgerahul24 Jul 13, 2021
e69aad0
Merge remote-tracking branch 'origin/master'
georgerahul24 Jul 13, 2021
ea0d4e6
Update basics/answers.md
georgerahul24 Jul 13, 2021
69e62b3
updated handy-stuff-strings.md
georgerahul24 Jul 13, 2021
34693ce
Merge branch 'master' of https://github.com/georgerahul24/python-tuto…
georgerahul24 Jul 13, 2021
b2e23df
updated handy-stuff-strings.md
georgerahul24 Jul 13, 2021
7e79089
Update basics/answers.md
georgerahul24 Jul 30, 2021
8cbe80f
Update basics/answers.md
georgerahul24 Jul 30, 2021
0e407c3
Update basics/answers.md
georgerahul24 Jul 30, 2021
228c862
Update basics/answers.md
georgerahul24 Jul 30, 2021
ea2f5f7
Update basics/answers.md
georgerahul24 Jul 30, 2021
b9ef729
Update basics/answers.md
georgerahul24 Jul 30, 2021
f8549e8
Update basics/answers.md
georgerahul24 Jul 30, 2021
985a52f
Update basics/handy-stuff-strings.md
georgerahul24 Jul 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The instructions and text in this tutorial (the "software") are licensed
under the zlib License.

(C) 2016-2018 Akuli
(C) 2016-2021 Akuli

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand Down
42 changes: 40 additions & 2 deletions basics/answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
print("Access denied.")
```

Again, this is not a good way to ask a real password from the user.
Again, this is not a good way to ask a real password from the user.

## Handy stuff: Strings

Expand Down Expand Up @@ -156,7 +156,15 @@ isn't exactly like mine but it works just fine it's ok, and you can
print(message, "!!!")
print(message, "!!!")
```

3. In the code below, `palindrome_input[::-1]` is the string `palindrome_input` reversed.
For example, if `palindrome_input` is `"hello"`, then `palindrome_input[::-1]` is `"olleh"`.
```python
palindrome_input=input("Type the number to check:")
if palindrome_input == palindrome_input[::-1]:
print("This string is a palindrome")
else:
print("This string is not a palindrome")
```
## Lists and tuples

1. Look carefully. The `namelist` is written in `()` instead of `[]`,
Expand Down Expand Up @@ -296,6 +304,36 @@ isn't exactly like mine but it works just fine it's ok, and you can
print(converted_numbers)
```

5. ``` python
row_count = int(input("Type the number of rows needed:"))
for column_count in range(1, row_count+1):
# Print numbers from 1 to column_count
for number in range(1, column_count+1):
print(number, end=" ")
print() # creates a new line for the next row
```
If the user enters 5, we want to do a row with 1 column, then 2 columns, and so on until 5 columns.
That would be `for column_count in range(1, 6)`, because the end of the range is excluded.
In general, we need to specify `row_count + 1` so that it actually ends at `row_count`.
The second loop is similar.

Usually `print(number)` puts a newline character at the end of the line, so that the next print goes to the next line.
To get all numbers on the same line, we use a space instead of a newline character,
but we still need `print()` to add a newline character once we have printed the entire row.



6. ```python
row_count=int(input("Type the number of rows needed:"))

for line_number in range(1, row_count + 1):
for number in range(line_number, row_count+1):
print(number, end=' ')
print()
```
Just like in the previous exercise, if the user enters 5, the first `for` loop gives the line numbers `1, 2, 3, 4, 5`.<br>
For example, on line 2, we should print numbers from 2 to 5, as in `range(2, 6)`, or in general, `range(line_number, row_count+1)`.

## Trey Hunner: zip and enumerate

1. Read some lines with `input` into a list and then enumerate it.
Expand Down
2 changes: 2 additions & 0 deletions basics/handy-stuff-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ ValueError: could not convert string to float: 'hello'
print(message, "!!!")
print(message, "!!!")
```
3. Make a program to take the input from the user and check if it is a palindrome.<br>
(Hint: A string is a palindrome if it is the same when reversed. Google how to reverse a string.)

The answers are [here](answers.md#handy-stuff-strings).

Expand Down
1 change: 1 addition & 0 deletions basics/if.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ you are asked to "fix code", feel free to add missing blank lines.
the user entered the correct password, a wrong password, or nothing
at all by pressing Enter without typing anything.


The answers are [here](answers.md#if-else-and-elif).

***
Expand Down
20 changes: 19 additions & 1 deletion basics/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,25 @@ while True:
number = int(number)
print(numbers)
```

5. Make a program that prints a pyramid like shown below. Ask the user to type the number of rows needed.
```
OUTPUT for 5 rows
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```
6. Make a program to get a pyramid like shown below where user can type the number of rows needed.
```
OUTPUT for 5 rows
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
```
The answers are [here](answers.md#loops).
***
Expand Down