Skip to content

Commit

Permalink
some more stylistic edits to lessons 0,1,2
Browse files Browse the repository at this point in the history
admonitions for problems look better without the 'Problem' title, making all multiline codeblocks numbered.. except standard outputs, to keep them visually distinct from the actual 'code' blocks
  • Loading branch information
aravinds-arv committed Oct 20, 2023
1 parent 298721d commit 669c5bd
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 109 deletions.
2 changes: 1 addition & 1 deletion docs/chapter-0/lesson-0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## What is Python?
To answer this question, we need to first ask _What is a programming language?_ A **programming language** is essentially a language in which you can tell your computer what to do. You will understand what that means as start programming. Every programming language has its own strengths and purposes. **Python** is a general-purpose programming language that can be used for a wide variety of purposes including but not limited to data science, automation, machine learning and software and web development. A typical python program to find the sum of the first 100 numbers looks like this:

```python
```python linenums="1"
sum = 0
for number in range(1, 101):
sum += number
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter-1/lesson-1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Hello World!

The sentence enclosed by the parentheses of the `#!py print()` function is called a **string**. A **string** is a sequence of characters enclosed in quotes. Strings can either be in single quotes or double quotes. However, a single quote can't be matched against a double quote to enclose a string. We have used single quotes in line 1 and double quotes in line 3. Both lines give identical outputs. The ability to use both single quotes and double quotes comes in handy in situations like this:

!!! question "Problem"
!!! question " "
Print a string that has an apostrophe in it:

```pycon
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter-1/lesson-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The interpreter throws a `TypeError`. The message accompanying the error is more

The next exception that we will frequently encounter is `NameError`.

```python
```python linenums="1"
print('There is no problem with this line')
print(x ** 2)
```
Expand Down
32 changes: 16 additions & 16 deletions docs/chapter-1/lesson-1.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ third line

The following code will throw a `SyntaxError`:

```python
```python linenums="1"
x = 'first line
second line
third line'
Expand All @@ -35,7 +35,7 @@ print(x)

This is where `'''` comes in:

```python
```python linenums="1"
x = '''first line
second line
third line'''
Expand All @@ -56,21 +56,21 @@ The `\n` character that you see above is called a newline character. Head to the

The length of a string is the number of characters in it. Python provides a built-in function called `len` to find the length of a string:

```python
```python linenums="1"
x = 'good'
print(len(x))
```

The code given above will give 4 as the output. If you are familiar with other programming languages, such as C, you might be aware of a character data type. Python doesn't have a separate data type for characters. A character in Python is represented by a string of length 1. In the following examples, `x` and `y` are strings of length 1.

```python
```python linenums="1"
x = 'a'
y = 'b'
```

We can also define empty strings:

```python
```python linenums="1"
x = ''
print(len(x))
```
Expand All @@ -85,7 +85,7 @@ As expected, the length of the empty string is 0.

We can concatenate two strings using the `+` operator. Concatenation is just a fancy term for joining two strings together:

```python
```python linenums="1"
string1 = 'first'
string2 = ','
string3 = 'second'
Expand All @@ -105,7 +105,7 @@ first,second

We can make multiple copies of a string and string them all together using the `*` operator:

```python
```python linenums="1"
s = 'good'
five_s = s * 5
print(five_s)
Expand All @@ -119,7 +119,7 @@ goodgoodgoodgoodgood

The `*` operator has made the string look too good! This is a fine demonstration of that ancient adage: "multiplication is repeated addition":

```python
```python linenums="1"
s = 'good'
s * 5 == s + s + s + s + s # This expression evaluates to True
```
Expand All @@ -130,7 +130,7 @@ s * 5 == s + s + s + s + s # This expression evaluates to True

We can compare two strings. To begin with, we have the `==` operator:

```python
```python linenums="1"
x = 'python'
print(x == 'python', x == 'nohtyp')
```
Expand All @@ -143,7 +143,7 @@ True False

Two strings are equal if and only if both of them represent exactly the same sequence of characters. Now, consider the following lines of code:

```python
```python linenums="1"
print('good' > 'bad')
print('nine' < 'one' )
print('a' < 'ab' < 'abc' < 'b')
Expand All @@ -169,14 +169,14 @@ Python’s string type uses the Unicode standard for representing characters, wh

Python provides a built-in function called `ord` that returns the code point of any given character. For example:

```python
```python linenums="1"
print(ord('a'), ord('b'))
print(ord('a'), ord('A'))
```

The output is:

```python
```
97 98
97 65
```
Expand All @@ -189,7 +189,7 @@ Now, we clearly see why `'a' < 'b'` returns `True`. This is because the code poi

In Python, the backslash - `\` - is called the escape character. One of its uses is to represent certain white-space characters such as tabs and newlines. We will look at them one by one using the following examples:

```python
```python linenums="1"
print('This is the first sentence.\nThis is the second sentence.')
```

Expand All @@ -202,7 +202,7 @@ This is the second sentence.

`\n` is a newline character. Its effect is to introduce a new line. Note that even though there are two separate characters: `\` and `n`, `\n` is still regarded as a single character. To verify this, execute the following code. You should get 1 as the output.

```python
```python linenums="1"
x = '\n'
print(len(x))
```
Expand Down Expand Up @@ -239,7 +239,7 @@ Now remove the backslash from the above string and try to print it. You will get

A string is a substring of another string if the first string is contained in the second. For example, `'good'` is a substring of `'very good'`, whereas `'very good'` is not a substring of `'verygood'`. Python provides a keyword - `in` - which can be used to check if a given string is a substring of another string. For example:

```python
```python linenums="1"
a = 'good'
b = 'very good'
present = a in b
Expand All @@ -257,7 +257,7 @@ False

`in` is a powerful keyword which has several other uses. It can also be used along with `not` in the following manner:

```python
```python linenums="1"
a = 'abc'
b = 'ab'
print(a not in b)
Expand Down
32 changes: 16 additions & 16 deletions docs/chapter-1/lesson-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Given a word such as "world", we say that 'w' is the first letter in the word, '

Once this is defined, we can go ahead and access characters that are at a given position in a string:

```python
```python linenums="1"
word = 'world'
print(word[0])
print(word[1])
Expand All @@ -45,7 +45,7 @@ d

Given a variable, say `word`, that holds a string literal, `word[i]` gives the character at index `i` in the string. Informally, this would be the letter at position `i + 1` in the string. Now, let us turn to the following code:

```python
```python linenums="1"
word = 'world'
print(word[5])
```
Expand All @@ -61,7 +61,7 @@ IndexError: string index out of range

The interpreter throws an `IndexError` as we are trying to access an index that is out of range. The length of the string is `5`. Since we start the index from `0`, the last character will be at index `4`. Anything greater than that is going to throw an error. Now, let us turn to the other end of the spectrum:

```python
```python linenums="1"
word = 'world'
print(word[-1])
```
Expand All @@ -87,7 +87,7 @@ Image credit: [Wikipedia](https://en.wikipedia.org/wiki/Penrose_stairs#/media/Fi

An index of `-1` points to the last element in the sequence. From this, we keep moving backwards until we reach the first element in the sequence which is at index `-5`.

```python
```python linenums="1"
word = 'world'
print(word[-1])
# ... please add the remaining lines!
Expand Down Expand Up @@ -118,7 +118,7 @@ Given a string, we would like to extract the roll number of the student from it.

![slicing](../assets/images/img-030.png)

```python
```python linenums="1"
email = 'CS_10_014@iitm.ac.in'
roll = email[6 : 9]
print(roll)
Expand All @@ -128,7 +128,7 @@ The slicing operator - `start:stop` - will be our knife in slicing sequences! L

Few more examples using the same string:

```python
```python linenums="1"
email = 'CS_10_014@iitm.ac.in'
branch = email[0 : 2]
year = email[3 : 5]
Expand All @@ -139,15 +139,15 @@ college = email[10 : 14]

Slicing is quite powerful. If we want the institute roll number, including the branch, we could do the following:

```python
```python linenums="1"
email = 'CS_10_014@iitm.ac.in'
in_roll = email[ : 9]
print(in_roll)
```

This outputs `CS_10_014`. If no starting index is specified in the slice, then `start` will default to `0`. Likewise, if no stopping index is specified, `stop` will default to the end of the string or `len(email)`. Now, consider:

```python
```python linenums="1"
email = 'CS_10_014@iitm.ac.in'
domain = email[-10 : ]
print(domain)
Expand All @@ -159,7 +159,7 @@ This outputs `iitm.ac.in`. Think for a while about the output. It is just a comb

Using the above visual, we can now very easily process the following slices:

```python
```python linenums="1"
word = 'world'
print(word[-4 : 3])
print(word[1 : -2])
Expand All @@ -171,7 +171,7 @@ print(word[1 : -2])

Execute the following code and observe the output:

```python
```python linenums="1"
word = 'some string'
word[0] = 'S'
```
Expand All @@ -180,7 +180,7 @@ The interpreter throws a `TypeError` with the following error message: `'str' ob

Note that this is different from the following:

```python
```python linenums="1"
word = 'some string'
word = 'Some string'
```
Expand All @@ -197,22 +197,22 @@ The number on the arrow represents the line number in the code. `word` binds to

Consider the following problem:

!!! question "Problem"
!!! question " "
Accept a sentence as input from the user and output the same sentence with the first letter in the sentence capitalized.

For example, if the input is `'this is a chair.'`, the output should be `'This is a chair.'`.

**Solution**

```python
```python linenums="1"
sentence = input()
cap_sentence = sentence.capitalize()
print(cap_sentence)
```

`capitalize` is called a method. Methods are essentially functions, but they are defined for specific objects. So, they have to be called by using the object for which they have been defined. In the case of `capitalize`, it is a method that is defined for the `str` data type. If we try to call it using an `int` object, we will get an error:

```python
```python linenums="1"
##### Alarm! Wrong code snippet!
a = 1
a.capitalize()
Expand All @@ -221,12 +221,12 @@ a.capitalize()

Getting back to the previous code snippet, `sentence.capitalize()` returns a string, which is then assigned to a new variable called `cap_sentence`. There are plenty of other methods associated with strings. Let us look at one more method which features in the solution to this interesting problem:

!!! question "Problem"
!!! question " "
Check whether a given string is a valid name of a person.

It is safe to assume that we are not thinking about Elon Musk's son, in which case, a name usually has only alphabets without any special characters and numbers. The method `isalpha` checks for just this requirement:

```python
```python linenums="1"
# name is some pre-defined string
valid = name.isalpha()
print(valid)
Expand Down
Loading

0 comments on commit 669c5bd

Please sign in to comment.