Skip to content

Commit 6521701

Browse files
authored
Merge pull request #10 from DIYA-bot/DIYA-bot-patch-10
Update python-casting.md
2 parents 68a46ad + dadad7c commit 6521701

File tree

1 file changed

+11
-80
lines changed

1 file changed

+11
-80
lines changed

docs/python/python-casting.md

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,28 @@ sidebar_position: 6
66
tags:
77
[
88
Python,
9-
Introduction of python,
10-
Python Syntax,
11-
Python Variables,
12-
Python Operators,
139
Type Casting,
14-
10+
Type Conversion,
11+
Python Data Types,
12+
int(),
13+
float(),
14+
str(),
1515
]
16-
16+
description: Learn how to convert a variable from one data type to another in Python using the built-in int(), float(), and str() functions.
1717
---
1818

1919
# Python Casting
2020

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.
21+
In Python, **casting** is the process of converting a variable from one data type to another. Python has built-in functions for converting between data types.
2222

2323
---
2424

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-
25+
## `int()` - Integer Casting
3626

37-
### `int()` - Integer Casting
38-
39-
Converts a value to an integer. Works with floats and numeric strings.
27+
Converts a value to an integer. This function truncates floats (it does not round) and works with numeric strings.
4028

4129
```python
4230
x = int(1) # 1
43-
y = int(2.8) # 2
31+
y = int(2.8) # 2 (value is truncated)
4432
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.
33+
# w = int("abc") # Raises a ValueError

0 commit comments

Comments
 (0)