Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Add input #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions 2__python_self_study_1/Add input
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

# Explanation of str(input()), int(input()), and float(input())

This document explains how to use `str(input())`, `int(input())`, and `float(input())` in Python with simple examples.

---

## **1. `str(input())`**
- **Explanation:**
- `input()` collects data from the user as a string by default.
- Wrapping it in `str()` ensures the input is treated as a string explicitly.
- Useful when working with text.

**Example:**
```python
name = str(input("Enter your name: ")) # User enters: Obay
print("Hello, " + name) # Output: Hello, Obay
```

---

## **2. `int(input())`**
- **Explanation:**
- `input()` collects data as a string, but `int()` converts it into an integer.
- Use this for numbers that don’t have decimals (whole numbers).
- If the user enters a non-numeric value (like "abc"), it will throw an error.

**Example:**
```python
age = int(input("Enter your age: ")) # User enters: 25
print("You are " + str(age) + " years old.") # Output: You are 25 years old.
```

---

## **3. `float(input())`**
- **Explanation:**
- `input()` collects data as a string, but `float()` converts it into a decimal (floating-point number).
- Use this for numbers that might include decimals.

**Example:**
```python
price = float(input("Enter the price: ")) # User enters: 12.99
print("The price is $" + str(price)) # Output: The price is $12.99
```

---

## **Key Notes**
1. Always use the correct type (`int` for whole numbers, `float` for decimals, and `str` for text).
2. If the input can't be converted (e.g., entering text when `int()` is expected), Python will show an error.
3. You can also use `type()` to verify the data type of user input.

**Example:**
```python
value = int(input("Enter a number: "))
print("Type:", type(value)) # Output: <class 'int'>
```