From a401baf0e91ce8f557126df45db5f86f48ea8c04 Mon Sep 17 00:00:00 2001 From: Dhrubaraj Pati Date: Fri, 13 Jun 2025 18:23:42 +0530 Subject: [PATCH 1/2] add Python Type Casting chapter to tutorial --- docs/python/python-casting.md | 103 +++++++++++++++++++++++++++++++ docs/python/setup-environment.md | 2 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 docs/python/python-casting.md diff --git a/docs/python/python-casting.md b/docs/python/python-casting.md new file mode 100644 index 00000000..e483bb0a --- /dev/null +++ b/docs/python/python-casting.md @@ -0,0 +1,103 @@ +--- +id: python-casting +title: Type Casting +sidebar_label: Type Casting #displays in sidebar +sidebar_position: 6 +tags: + [ + Python, + Introduction of python, + Python Syntax, + Python Variables, + Python Operators, + Type Casting, + + ] + +--- + +# Python Casting + +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. + +--- + +### Specify a Variable Type + +Python is an **object-oriented language**, and **variables are objects**. +You can specify the data type using casting functions: + +```python +x = int(1) # x will be 1 +y = int(2.8) # y will be 2 +z = int("3") # z will be 3 +```` + + +### `int()` - Integer Casting + +Converts a value to an integer. Works with floats and numeric strings. + +```python +x = int(1) # 1 +y = int(2.8) # 2 +z = int("3") # 3 +# w = int("abc") # ❌ Error +``` + + +### `float()` - Floating-Point Casting + +Converts a value to a float. Works with integers and numeric strings. + +```python +a = float(1) # 1.0 +b = float("2.5") # 2.5 +c = float(3.0) # 3.0 +``` + + +### `str()` - String Casting + +Converts numbers or other types into a string. + +```python +x = str("s1") # 's1' +y = str(2) # '2' +z = str(3.0) # '3.0' +``` + +### Invalid Casting + +Some values can't be casted directly: + +```python +int("hello") # ValueError +float("abc") # ValueError +``` + +Use `try`/`except` to handle safely: + +```python +value = "abc" +try: + number = int(value) +except ValueError: + print("Invalid conversion") +``` + +### Summary Table + +| Function | Converts to | Example Input | Output | +| --------- | ----------- | ------------- | ------- | +| `int()` | Integer | `"3"` | `3` | +| `float()` | Float | `"3.5"` | `3.5` | +| `str()` | String | `3.5` | `"3.5"` | + + +### Quick Notes + +* Use casting to convert types manually. +* Useful when handling user input, math, or data from files. +* Always validate input before casting to avoid errors. diff --git a/docs/python/setup-environment.md b/docs/python/setup-environment.md index 46ad96f1..cd0c8caa 100644 --- a/docs/python/setup-environment.md +++ b/docs/python/setup-environment.md @@ -2,7 +2,7 @@ id: setup-environment title: Setting up your development environment sidebar_label: Setting up environment -sidebar_position: 6 +sidebar_position: 7 tags: [ html, From b4ac215a884099c9312d63a9f3f384f4f2711fc9 Mon Sep 17 00:00:00 2001 From: Dhrubaraj Pati Date: Fri, 13 Jun 2025 18:26:21 +0530 Subject: [PATCH 2/2] docs: add Python Type Casting chapter to tutorial - Introduced casting using int(), float(), and str() - Covered examples for each conversion type - Included error handling and summary table - Follows structure similar to W3Schools Python Casting guide Co-Authored-By: Sanjay Viswanathan --- docs/python/python-casting.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/python/python-casting.md b/docs/python/python-casting.md index e483bb0a..a5ae390a 100644 --- a/docs/python/python-casting.md +++ b/docs/python/python-casting.md @@ -18,8 +18,7 @@ tags: # Python Casting -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. +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. --- @@ -43,7 +42,7 @@ Converts a value to an integer. Works with floats and numeric strings. x = int(1) # 1 y = int(2.8) # 2 z = int("3") # 3 -# w = int("abc") # ❌ Error +# w = int("abc") # Error ```