8000 Added slicing to session 2 · gelmok/intro-to-python@9864d07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9864d07

Browse files
committed
Added slicing to session 2
1 parent 0d32564 commit 9864d07

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

session_02/answers/A11.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 11. Print out the sentence from A10 but make the month upper case.
1+
# A11 - Print out the sentence from A10 but make the month upper case.
22

33
# Ask the user for input for the month and the temperatures
44
month = input("What month is it?\n")

session_02/answers/A12.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 12. Create a variable that holds your favourite animals and print it out.
1+
# A12 - Create a variable that holds your favourite animals and print it out.
22
# Make sure the animals are all on different lines and tabbed.
33

44
# Create a list of your favourite animals, using \n\t for the new line and new tab

session_02/answers/A13.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 13. Ask the user to enter their name as well as a number.
1+
# A13 - Ask the user to enter their name as well as a number.
22
# Print out the uppercase character at that position in the string.
33

44
# Ask the user for input

session_02/answers/A14.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A14 - Slice the string with steps of 2, excluding the first and last characters of the string "WelcometoPython"
2+
3+
4+
# creating a variable with the string in
5+
str = "WelcometoPython"
6+
7+
# finding out the length of the string to decide which characters to exclude
8+
print(len(str))
9+
10+
# slicing the string with steps of 2, excluding the first and last character
11+
print(str[1:14:2])

session_02/answers/A8.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Ask the user to enter their favourite colour and find out the length of the colour they input.
1+
# A8 - Ask the user to enter their favourite colour and find out the length of the colour they input.
22

33
# Ask the user for input
44
colour = input("What is your favourite colour?\n")

session_02/exercises/exercises_2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
11. Print out the above sentence but make the month upper case.
1515
12. Create a variable that holds your favourite animals and print it out. Make sure the animals are all on different lines and tabbed.
1616
13. Ask the user to enter their name as well as a number. Print out the uppercase character at that position in the string.
17+
14. Slice the string with steps of 2, excluding the first and last characters of the string "WelcometoPython"

session_02/slides/session_2.md

Lines changed: 24 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-1353e054b26178449aad8429a20e3f3543d93fd61b1afe418a65fc5c30c5ac33-353-353-2" data-line-anchor="diff-1353e054b26178449aad8429a20e3f3543d93fd61b1afe418a65fc5c30c5ac33R353" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">

Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ print("HeLlO".lower()) # hello
351351

352352
---
353353
354+
# Slicing
355+
356+
---
357+
358+
# Slicing
359+
360+
```
361+
# slicing from one point to another
362+
"string"[0:6] #string
363+
"string"[1:4] #tri
364+
365+
# slicing from one point to the end
366+
"string"[0:] #string
367+
"string"[1:] #ing
368+
369+
# slicing from the start to another point
370+
"string"[:0] #
371+
"string"[:2] #st
372+
373+
# slicing from the start to end, by skipping step
374+
"string"[0:6:2] #srn
375+
```
376+
---
377+
354378
# Putting it together
355379

356380
```python

0 commit comments

Comments
 (0)
0