Skip to content

Commit 5a6db16

Browse files
authored
Merge pull request #5 from gauravfs-14/main
[Add]: Strings, Numeric Representation
2 parents a41c3bf + 93bb900 commit 5a6db16

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# Numeric Representation
6+
7+
Python supports different ways of representing numbers, including integer and real (floating point) numbers.
8+
9+
## Integer Types (int)
10+
11+
Integers in Python can be represented in various numerical systems:
12+
13+
- **Decimal Notation (Base 10):**
14+
15+
- This is the default way we represent numbers, such as `3` or `-89`.
16+
17+
- **Binary Notation (Base 2):**
18+
19+
- Represented using a prefix of `0b`. It consists only of `0` and `1`.
20+
- Example: `0b1011` is equivalent to `11` in decimal.
21+
22+
- **Octal Notation (Base 8):**
23+
24+
- Represented using a prefix of `0o`. It uses digits from `0` to `7`.
25+
- Example: `0o12` is equivalent to `10` in decimal.
26+
27+
- **Hexadecimal Notation (Base 16):**
28+
- Represented using a prefix of `0x`. It uses digits from `0` to `9` and letters `A` to `F` to represent values `10` to `15`.
29+
- Example: `0x2B` is equivalent to `43` in decimal.
30+
31+
### Conversion between Notations
32+
33+
- To **convert binary to decimal**, sum each digit multiplied by `2` raised to the power of its position, starting from `0` on the right.
34+
35+
- Example: `0b1011` in decimal is calculated as:
36+
- \( 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 8 + 0 + 2 + 1 = 11 \)
37+
38+
- To **convert octal to decimal**, use powers of `8`.
39+
40+
- Example: `0o52` in decimal is calculated as:
41+
- \( 5 \times 8^1 + 2 \times 8^0 = 40 + 2 = 42 \)
42+
43+
- To **convert hexadecimal to decimal**, use powers of `16`.
44+
- Example: `0x2B` in decimal is calculated as:
45+
- \( 2 \times 16^1 + 11 \times 16^0 = 32 + 11 = 43 \)
46+
47+
Python can perform these conversions using built-in functions:
48+
49+
- `bin(value)` returns the binary representation as a string.
50+
- `oct(value)` returns the octal representation as a string.
51+
- `hex(value)` returns the hexadecimal representation as a string.
52+
53+
To **convert a string representation** back to an integer, use the `int()` function.
54+
55+
- Example: `int("0b1011", 2)` converts the binary string to decimal.
56+
57+
### Integer Variables in Different Notations
58+
59+
When working with different notations, Python internally treats the value as an integer regardless of how it is expressed.
60+
61+
- Example:
62+
```python title="numbers.py"
63+
a = 10
64+
b = 0b1010
65+
c = 0o12
66+
d = 0xA
67+
print(a, b, c, d) # Outputs: 10 10 10 10
68+
```
69+
All four variables have the same value, `10`, but are written in different notations.
70+
71+
## Floating-Point Numbers (float)
72+
73+
Floating-point numbers represent real numbers and can have a fractional part.
74+
75+
- **Standard Notation:**
76+
77+
- Examples: `3.0`, `-89.14`.
78+
79+
- **Scientific Notation:**
80+
- Represented using `e` or `E` to indicate powers of `10`.
81+
- Example: `1.5e3` is equivalent to \( 1.5 \times 10^3 = 1500 \).
82+
83+
### Precision Issues with Floating-Point Numbers
84+
85+
- Floating-point numbers may have **precision issues** because they cannot precisely represent all real numbers due to limitations in memory representation.
86+
- Example:
87+
```python title="numbers.py"
88+
a = 0.1
89+
b = 0.2
90+
sum = a + b
91+
print("The sum is", sum) # Outputs: The sum is 0.30000000000000004
92+
```
93+
Instead of `0.3`, Python returns a value close to `0.3` due to rounding errors in binary representation.
94+
95+
### Addressing Precision Issues
96+
97+
- For cases where precision is critical (e.g., financial calculations), Python provides the `decimal` module, which offers more accurate representation and operations. This module is beyond the scope of the video.
98+
99+
## Practical Examples
100+
101+
- **Printing Variables in Different Notations:**
102+
103+
```python title="numbers.py"
104+
a = 10
105+
b = 0b1010
106+
c = 0o12
107+
d = 0xA
108+
print(bin(a), oct(b), hex(c), hex(d)) # Outputs: 0b1010 0o12 0xa 0xa
109+
```
110+
111+
This example prints the binary, octal, and hexadecimal representations of the value `10`.
112+
113+
- **Floating-Point Precision Example:**
114+
```python title="numbers.py"
115+
a = 0.1
116+
b = 0.2
117+
sum = a + b
118+
print(f"The sum is approximately: {sum:.2f}") # Outputs: The sum is approximately: 0.30
119+
```
120+
By formatting the output, we can display the sum as `0.30`, but the underlying value still contains precision errors.

docs/python-basics/stings.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
sidebar_position: 6
3+
---
4+
5+
# Strings
6+
7+
## Definition of a String
8+
9+
- A **string** is a sequence of characters, essentially text data. In Python, a string is represented by enclosing characters within either **single** (`'`) or **double quotes** (`"`).
10+
- Example:
11+
```python title="strings.py"
12+
name = "John"
13+
greeting = 'Hello, world!'
14+
```
15+
16+
## Creating Strings
17+
18+
- Strings can be assigned to variables:
19+
```python title="strings.py"
20+
city = "San Francisco"
21+
```
22+
- **Double vs. Single Quotes**:
23+
- You can use both single or double quotes to create strings.
24+
- The opening and closing quotes must match.
25+
- Example:
26+
```python title="strings.py"
27+
sentence = 'It\'s a sunny day'
28+
# Alternatively, using double quotes avoids the need to escape the single quote:
29+
sentence = "It's a sunny day"
30+
```
31+
32+
## Multi-line Strings
33+
34+
- For **multi-line strings**, use **triple quotes** (`'''` or `"""`).
35+
- Example:
36+
```python title="strings.py"
37+
address = """123 Elm Street
38+
Springfield
39+
USA"""
40+
```
41+
- This allows the string to span multiple lines.
42+
43+
## String Concatenation
44+
45+
- You can **concatenate** (join) strings using the **`+` operator**:
46+
```python title="strings.py"
47+
full_name = "John" + " " + "Doe"
48+
# Output: "John Doe"
49+
```
50+
- Note that **spaces** between words must be explicitly added, as shown.
51+
52+
## String Length
53+
54+
- Use the **`len()` function** to get the length of a string:
55+
```python title="strings.py"
56+
length = len(full_name)
57+
print("Length of the full name:", length)
58+
# Output: 8
59+
```
60+
61+
## String Repetition
62+
63+
- You can **repeat** a string using the `*` operator:
64+
```python title="strings.py"
65+
dash_line = "-" * 5
66+
# Output: "-----"
67+
```
68+
69+
## String Methods
70+
71+
- **`upper()`**: Converts all characters to uppercase.
72+
```python title="strings.py"
73+
greeting = "hello"
74+
print(greeting.upper()) # Output: "HELLO"
75+
```
76+
- **`lower()`**: Converts all characters to lowercase.
77+
```python title="strings.py"
78+
print(greeting.lower()) # Output: "hello"
79+
```
80+
- **`title()`**: Converts the first character of each word to uppercase.
81+
```python title="strings.py"
82+
title_greeting = "welcome to python"
83+
print(title_greeting.title()) # Output: "Welcome To Python"
84+
```
85+
86+
## Whitespace Removal
87+
88+
- **`strip()`**: Removes leading and trailing whitespace.
89+
```python title="strings.py"
90+
spaced_string = " Hello "
91+
print(spaced_string.strip()) # Output: "Hello"
92+
```
93+
- **`lstrip()`**: Removes leading whitespace only.
94+
- **`rstrip()`**: Removes trailing whitespace only.
95+
96+
## Replacing Substrings
97+
98+
- **`replace(old, new)`**: Replaces all occurrences of `old` with `new` in the string.
99+
```python title="strings.py"
100+
text = "I like apples"
101+
new_text = text.replace("apples", "bananas")
102+
print(new_text) # Output: "I like bananas"
103+
```
104+
105+
## Finding Substrings
106+
107+
- **`count(sub)`**: Counts how many times a substring appears in the string.
108+
```python title="strings.py"
109+
phrase = "banana"
110+
print(phrase.count("a")) # Output: 3
111+
```
112+
- **`find(sub)`**: Returns the index of the first occurrence of the substring. If not found, returns `-1`.
113+
```python title="strings.py"
114+
print(phrase.find("na")) # Output: 2
115+
```
116+
117+
## Accessing Characters in a String
118+
119+
- You can **access individual characters** in a string using **indexing** with square brackets.
120+
- **Index starts at `0`** (first character):
121+
```python title="strings.py"
122+
first_char = phrase[0] # Output: 'b'
123+
```
124+
- **Negative indexing** starts from the end:
125+
```python title="strings.py"
126+
last_char = phrase[-1] # Output: 'a'
127+
```
128+
129+
## Comments and Disabling Code
130+
131+
- Use the **`#` symbol** to add comments to your code.
132+
- Example:
133+
```python title="strings.py"
134+
# This is a comment
135+
print("Hello, World!") # This line prints a greeting
136+
```
137+
- You can **comment out lines of code** to prevent them from executing, especially useful during debugging:
138+
```python title="strings.py"
139+
# print("This line won't run")
140+
print("This line will run")
141+
```
142+
143+
## String Slicing
144+
145+
- You can extract **substrings** using slicing syntax `[start:end]`:
146+
```python title="strings.py"
147+
text = "Python programming"
148+
sub_text = text[0:6] # Output: "Python"
149+
```
150+
- **`start`** is inclusive, **`end`** is exclusive.
151+
152+
This overview gives a foundational understanding of working with strings in Python, covering creating, manipulating, and accessing string data effectively.

0 commit comments

Comments
 (0)