0% found this document useful (0 votes)
2 views

basic functions

The document introduces several useful Python functions for coding, including `input()`, `int()`, `float()`, `len()`, `str()`, `max()`, `min()`, `type()`, `round()`, `abs()`, `sum()`, and `range()`. Each function is explained with examples demonstrating its usage and purpose, such as getting user input, converting data types, and performing calculations. Additionally, a practice challenge is provided to encourage users to apply these functions in a small program.

Uploaded by

tazeem12
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

basic functions

The document introduces several useful Python functions for coding, including `input()`, `int()`, `float()`, `len()`, `str()`, `max()`, `min()`, `type()`, `round()`, `abs()`, `sum()`, and `range()`. Each function is explained with examples demonstrating its usage and purpose, such as getting user input, converting data types, and performing calculations. Additionally, a practice challenge is provided to encourage users to apply these functions in a small program.

Uploaded by

tazeem12
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Absolutely!

Let’s dive into some more useful Python functions that you'll use frequently as you start
coding. These functions will help you work with data, interact with the user, and perform common tasks.

### 1. **`input()` – Getting User Input**

The `input()` function allows you to get input from the user, which can be really useful for interactive
programs. It always returns the input as a string.

```python

name = input("What is your name? ") # Prompts the user for input

print("Hello, " + name + "!")

```

- The `input()` function will show the message inside the parentheses to the user and wait for them to
type something. Once they do, it returns their input as a string.

### 2. **`int()` and `float()` – Converting Data Types**

If you want to turn a string (like the one from `input()`) into a number so you can perform math, you can
use `int()` (for integers) or `float()` (for decimal numbers).

```python

age = input("How old are you? ") # This will be a string

age = int(age) # Converts the string into an integer

print("In 5 years, you will be", age + 5) # Adding 5 to the number

```

- `int()` will convert strings like `"5"` into the number `5`.

- `float()` is used to convert a string like `"3.14"` into a decimal number.

### 3. **`len()` – Finding the Length of a String or List**

The `len()` function is used to find the length of a string or the number of items in a list.
```python

word = "Python"

print(len(word)) # This will print 6, because "Python" has 6 characters

fruits = ["apple", "banana", "cherry"]

print(len(fruits)) # This will print 3, because there are 3 items in the list

```

### 4. **`str()` – Converting to String**

Sometimes you might want to convert a number or other data type into a string (for example, when
you’re combining it with other text). You can use the `str()` function for that.

```python

age = 25

message = "Your age is " + str(age) # Convert the number to a string before combining

print(message)

```

### 5. **`max()` and `min()` – Finding the Largest and Smallest Values**

If you have a list of numbers, you can use `max()` to find the largest value and `min()` to find the
smallest.

```python

numbers = [10, 25, 7, 35, 18]

print(max(numbers)) # This will print 35

print(min(numbers)) # This will print 7

```
### 6. **`type()` – Checking the Type of a Value**

If you’re not sure what type of data something is, you can use `type()` to find out. This can be helpful
when debugging or working with user input.

```python

number = 42

print(type(number)) # <class 'int'> because it's an integer

text = "Hello!"

print(type(text)) # <class 'str'> because it's a string

```

### 7. **`round()` – Rounding Numbers**

If you have a decimal number and want to round it, `round()` is useful. You can also specify how many
decimal places you want to round to.

```python

pi = 3.14159

print(round(pi, 2)) # Rounds to two decimal places: 3.14

```

### 8. **`abs()` – Getting the Absolute Value**

The `abs()` function returns the absolute value of a number, which means it removes the negative sign if
the number is negative.

```python

negative_number = -5

print(abs(negative_number)) # This will print 5

```
### 9. **`sum()` – Adding Up Numbers in a List**

If you have a list of numbers and want to find the total, you can use `sum()`.

```python

numbers = [1, 2, 3, 4, 5]

print(sum(numbers)) # This will print 15, the sum of all numbers

```

### 10. **`range()` – Creating a Sequence of Numbers**

The `range()` function generates a sequence of numbers, often used in loops.

```python

for i in range(5):

print(i)

```

- This will print the numbers from 0 to 4.

- You can also specify a starting point and a step:

```python

for i in range(1, 10, 2):

print(i)

```

This will print `1, 3, 5, 7, 9` because it starts at `1` and goes up to `10` in steps of `2`.

---

### Practice Challenge:


Now, let’s put some of these functions together! Try writing a small program that:

1. Asks the user for their name and age.

2. Tells them how old they will be in 10 years.

3. Prints the length of their name.

4. Converts their age into a string and displays a message with that string.

---

Let me know if you need help or clarification on anything, or if you're ready to move on to new
concepts!

You might also like