Skip to content

Commit 31aa57c

Browse files
authored
Merge pull request #100 from codewithdhruba01/doc/custing
docs: add Python Type Casting chapter to tutorial
2 parents 12426e0 + b4ac215 commit 31aa57c

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

docs/python/python-casting.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
id: python-casting
3+
title: Type Casting
4+
sidebar_label: Type Casting #displays in sidebar
5+
sidebar_position: 6
6+
tags:
7+
[
8+
Python,
9+
Introduction of python,
10+
Python Syntax,
11+
Python Variables,
12+
Python Operators,
13+
Type Casting,
14+
15+
]
16+
17+
---
18+
19+
# Python Casting
20+
21+
In Python, **casting** is the process of converting a variable from one type to another. Python has built-in functions for converting between data types.
22+
23+
---
24+
25+
### Specify a Variable Type
26+
27+
Python is an **object-oriented language**, and **variables are objects**.
28+
You can specify the data type using casting functions:
29+
30+
```python
31+
x = int(1) # x will be 1
32+
y = int(2.8) # y will be 2
33+
z = int("3") # z will be 3
34+
````
35+
36+
37+
### `int()` - Integer Casting
38+
39+
Converts a value to an integer. Works with floats and numeric strings.
40+
41+
```python
42+
x = int(1) # 1
43+
y = int(2.8) # 2
44+
z = int("3") # 3
45+
# w = int("abc") # Error
46+
```
47+
48+
49+
### `float()` - Floating-Point Casting
50+
51+
Converts a value to a float. Works with integers and numeric strings.
52+
53+
```python
54+
a = float(1) # 1.0
55+
b = float("2.5") # 2.5
56+
c = float(3.0) # 3.0
57+
```
58+
59+
60+
### `str()` - String Casting
61+
62+
Converts numbers or other types into a string.
63+
64+
```python
65+
x = str("s1") # 's1'
66+
y = str(2) # '2'
67+
z = str(3.0) # '3.0'
68+
```
69+
70+
### Invalid Casting
71+
72+
Some values can't be casted directly:
73+
74+
```python
75+
int("hello") # ValueError
76+
float("abc") # ValueError
77+
```
78+
79+
Use `try`/`except` to handle safely:
80+
81+
```python
82+
value = "abc"
83+
try:
84+
number = int(value)
85+
except ValueError:
86+
print("Invalid conversion")
87+
```
88+
89+
### Summary Table
90+
91+
| Function | Converts to | Example Input | Output |
92+
| --------- | ----------- | ------------- | ------- |
93+
| `int()` | Integer | `"3"` | `3` |
94+
| `float()` | Float | `"3.5"` | `3.5` |
95+
| `str()` | String | `3.5` | `"3.5"` |
96+
97+
98+
### Quick Notes
99+
100+
* Use casting to convert types manually.
101+
* Useful when handling user input, math, or data from files.
102+
* Always validate input before casting to avoid errors.

docs/python/setup-environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: setup-environment
33
title: Setting up your development environment
44
sidebar_label: Setting up environment
5-
sidebar_position: 6
5+
sidebar_position: 7
66
tags:
77
[
88
html,

0 commit comments

Comments
 (0)