Skip to content

Commit aa92297

Browse files
committed
Additions
1 parent ab90faf commit aa92297

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<div align="center">
2+
<img src="https://github.com/Vibhav1207/Python-Course/blob/main/assets/comments-escape.png?raw=true">
3+
</div>
4+
5+
---
6+
7+
## 📚 Table of Contents
8+
9+
- [Comments](#-comments)
10+
- [Escape Sequences](#-escape-sequences)
11+
- [Print Statement](#-print-statement)
12+
13+
# 💭 Comments
14+
15+
A comment is a part of the coding file that the programmer does not want to execute. It is used to:
16+
- Explain a block of code
17+
- Avoid execution of specific code while testing
18+
19+
## Single-Line Comments
20+
To write a comment just add a '#' at the start of the line.
21+
22+
```python
23+
# This is a 'Single-Line Comment'
24+
print("This is a print statement.")
25+
26+
print("Hello World !!!") # Printing Hello World
27+
28+
print("Python Program")
29+
# print("Python Program") # This line won't execute
30+
```
31+
32+
## Multi-Line Comments
33+
You can use '#' at each line or use multiline strings (""")
34+
35+
```python
36+
# It will execute a block of code if a specified condition is true.
37+
# If the condition is false then it will execute another block of code.
38+
p = 7
39+
if (p > 5):
40+
print("p is greater than 5.")
41+
else:
42+
print("p is not greater than 5.")
43+
44+
# Using multiline strings
45+
"""This is an if-else statement.
46+
It will execute a block of code if a specified condition is true.
47+
If the condition is false then it will execute another block of code."""
48+
```
49+
50+
# 🔄 Escape Sequences
51+
52+
Escape sequences are special characters that can't be directly used in a string. They start with a backslash (\) followed by the character you want to insert.
53+
54+
```python
55+
# This will cause an error:
56+
# print("This doesnt "execute")
57+
58+
# This will work:
59+
print("This will \" execute")
60+
```
61+
62+
# 🖨️ Print Statement
63+
64+
The print statement in Python has several parameters that control its output:
65+
66+
```python
67+
print(object(s), sep=separator, end=end, file=file, flush=flush)
68+
```
69+
70+
## Parameters
71+
72+
- **object(s)**: Any object(s) to print (will be converted to string)
73+
- **sep='separator'**: Separator between objects (default is space)
74+
- **end='end'**: What to print at the end (default is '\n')
75+
- **file**: Object with write method (default is sys.stdout)
76+
- **flush**: Whether to forcibly flush the stream
File renamed without changes.

03.Type Casting/README.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<div align="center">
2+
<img src="https://github.com/Vibhav1207/Python-Course/blob/main/assets/typecasting.png?raw=true">
3+
</div>
4+
5+
---
6+
## 📚 Table of Contents
7+
8+
- [Type Casting](#-type-casting)
9+
- [Explicit Type Casting](#-explicit-type-casting)
10+
- [Implicit Type Casting](#-implicit-type-casting)
11+
12+
## 🔄 Type Casting
13+
14+
Type Casting is the conversion of one data type into another data type. Python provides various built-in functions for type conversion, such as `int()`, `float()`, `str()`, `ord()`, `hex()`, `oct()`, `tuple()`, `set()`, `list()`, and `dict()`.
15+
16+
There are two types of Type Casting in Python:
17+
1. Explicit Type Casting (Manual Conversion)
18+
2. Implicit Type Casting (Automatic Conversion)
19+
20+
## 🎯 Explicit Type Casting
21+
22+
Explicit Type Casting, also known as Type Conversion, is performed manually by the programmer using Python's built-in functions. This is done when you want to specifically convert one data type to another.
23+
24+
### Common Type Conversion Functions
25+
26+
```python
27+
# Converting string to integer
28+
string_num = "15"
29+
number = int(string_num) # number = 15
30+
31+
# Converting integer to float
32+
int_num = 7
33+
float_num = float(int_num) # float_num = 7.0
34+
35+
# Converting number to string
36+
num = 123
37+
string = str(num) # string = "123"
38+
```
39+
40+
### Example of Explicit Type Casting
41+
42+
```python
43+
# Adding string number with integer
44+
string = "15"
45+
number = 7
46+
string_number = int(string) # Converting string to integer
47+
sum = number + string_number
48+
print("The Sum of both the numbers is:", sum) # Output: The Sum of both the numbers is 22
49+
```
50+
51+
## 🔄 Implicit Type Casting
52+
53+
Implicit Type Casting is automatically performed by Python interpreter when:
54+
1. We perform operations between different data types
55+
2. Python needs to convert a smaller data type to a higher data type to prevent data loss
56+
57+
### Data Type Hierarchy
58+
Python follows a hierarchy in data types where some types are "higher" than others:
59+
- Boolean → Integer → Float → Complex
60+
61+
### Example of Implicit Type Casting
62+
63+
```python
64+
# Integer and Float addition
65+
a = 7 # Integer
66+
print(type(a)) # Output: <class 'int'>
67+
68+
b = 3.0 # Float
69+
print(type(b)) # Output: <class 'float'>
70+
71+
# Python automatically converts result to float
72+
c = a + b # Integer + Float = Float
73+
print(c) # Output: 10.0
74+
print(type(c)) # Output: <class 'float'>
75+
```
76+
77+
### Common Implicit Type Casting Scenarios
78+
79+
```python
80+
# Integer and Float
81+
result1 = 5 + 3.14 # Result is float (8.14)
82+
83+
# Integer and Complex
84+
result2 = 10 + 2j # Result is complex (10+2j)
85+
86+
# Boolean and Integer
87+
result3 = True + 1 # Result is integer (2)
88+
```
89+
90+
## 🔄 Collection Type Casting
91+
92+
Python allows type casting between different collection types (list, tuple, set, dict). Here are some common scenarios:
93+
94+
```python
95+
# Converting between lists and tuples
96+
my_list = [1, 2, 3]
97+
my_tuple = tuple(my_list) # (1, 2, 3)
98+
back_to_list = list(my_tuple) # [1, 2, 3]
99+
100+
# Converting to set (removes duplicates)
101+
duplicates_list = [1, 2, 2, 3, 3, 4]
102+
unique_set = set(duplicates_list) # {1, 2, 3, 4}
103+
104+
# Converting dictionary keys/values to list
105+
my_dict = {"a": 1, "b": 2}
106+
keys_list = list(my_dict.keys()) # ["a", "b"]
107+
values_list = list(my_dict.values()) # [1, 2]
108+
```
109+
110+
## 🌟 Real-World Applications
111+
112+
### 1. Data Processing
113+
```python
114+
# Converting CSV data to appropriate types
115+
user_id = "1001" # From CSV as string
116+
age = "25" # From CSV as string
117+
118+
# Converting to proper types for processing
119+
processed_id = int(user_id)
120+
processed_age = int(age)
121+
```
122+
123+
### 2. API Response Handling
124+
```python
125+
# Converting JSON response data
126+
api_response = {"temperature": "24.5", "is_sunny": "true"}
127+
128+
# Converting to appropriate types
129+
temp = float(api_response["temperature"])
130+
is_sunny = api_response["is_sunny"].lower() == "true"
131+
```
132+
133+
## ⚡ Performance Considerations
134+
135+
1. **Memory Usage**:
136+
- Converting large collections can consume significant memory
137+
- Consider using generators for large datasets
138+
139+
2. **Processing Time**:
140+
- Implicit casting is generally faster than explicit casting
141+
- Avoid unnecessary type conversions in loops
142+
143+
```python
144+
# Less efficient (type conversion in loop)
145+
for i in range(1000):
146+
str_num = str(i) # Unnecessary conversion
147+
process(str_num)
148+
149+
# More efficient (convert once)
150+
str_nums = [str(i) for i in range(1000)]
151+
for num in str_nums:
152+
process(num)
153+
```
154+
155+
## 🚨 Important Notes
156+
157+
1. Not all type conversions can be done implicitly. Python will raise a TypeError if the conversion is not possible.
158+
2. Explicit type casting gives you more control but can raise errors if the conversion is not valid.
159+
3. When working with numeric types, Python automatically converts to the type that can best preserve the information.
160+
4. String to number conversion must always be done explicitly using `int()` or `float()`.
161+
5. Always validate data before type casting to handle potential errors gracefully.
162+
6. Consider using specialized libraries (like `numpy`) for efficient numerical type conversions.
163+
7. Be cautious when converting floating-point numbers to integers as it may lead to data loss.

assets/comments-escape.png

110 KB
Loading

assets/typecasating.png

108 KB
Loading

0 commit comments

Comments
 (0)