Skip to content
Merged
Show file tree
Hide file tree
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
113 changes: 113 additions & 0 deletions docs/python-tutorial/chapters/04-built-in-functions-pt-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
description: Built-in Functions Part 1
---

# Built-in Functions Part 1

!!! note

This chapter will only cover how to use some built-in functions. So, for now, consider functions as
helper code which does some specific task and is pre-written for you to use. You will learn about
functions in great detail later in this tutorial, so don't worry about that for now.

Python has many built-in functions which you can use to make your life easier. In this section, we
will take a look at `print`, `input` and some type-casting functions in detail.

## `print()`

The `print` function is used to print/show something on the screen (also known as the terminal or
standard output). With this, you can write the first program that every programmer writes when they
learn a new language:

```python
print("Hello World!") # Output: Hello World!
```

This line of code will print `Hello World!` on your screen. You can also print any other data type
(such as `int`, `float`, or `bool`) using `print`.

```python
print("My name is Alex") # Output: My name is Alex
print(56) # Output: 56
print(3.14) # Output: 3.14
print(False) # Output: False
```

You can also print the data currently present in a variable using `print`.

```python
a = 5
print(a) # Output: 5
a = "python"
print(a) # Output: python
```

Each `print` statement prints on a new line. You can print multiple things using a single `print`
statement by separating them using commas (`,`), which will result in all the values being printed
on the same line separated by spaces.

```python
print("Value of pi is", 3.14) # Output: Value of pi is 3.14
```

!!! note

Keep in mind that this works only in `print` and CANNOT be used to combine different strings or
variables with strings otherwise.

## `input()`

The `input` function is used to take input from the user through the terminal (also known as the
standard input). It returns everything the user types until the user hits the `Enter`/ `Return` key
on the keyboard. You can also give a `str` to the `input` function which will be displayed on the
screen while taking the input. For example:

```python
input("Enter your name: ")
```

The above code will print `Enter your name: ` followed by a blinking cursor where you can type the
actual input. Now, to actually use the value the user entered, you need to assign the value returned
by the `input` function to a variable.

```python
name = input("Enter your name: ")
print(name)
```

The `input` function returns the input as a `str`, so the variable `name` will be of type `str`. The
output of above code will be whatever the user entered.

## Type Casting

Now you know how to take input from the user, but, the `input` function returns a `str` no matter
what the user types. So, for example, if the user types `54`, it will return a `str` (`"54"`), not
an `int` (`54`). So, if you want to use the number the user entered, you will need to convert the
`str` returned by `input` to an `int`. This process of converting one data type to another is called
**type casting**.

There are two types of type casting:

1. **Implicit Type Casting**: It is done automatically by Python when it is needed.
2. **Explicit Type Casting**: It is done manually by the programmer.

### Explicit Type Casting

Python has many built-in functions for explicit type casting, and they are named the same as the
data type they cast to. Here is the list of type-casting functions with the type they cast to:

| Function | Type |
|-----------|------|
| `bool()` | `bool` |
| `float()` | `float` |
| `int()` | `int` |
| `str()` | `str` |

For example, you can do this to get a number from the user:

```python
number_string = input("Enter a number: ")
number_int = int(number_string)
```

Similarly, you can use other functions according to the data type you need.
2 changes: 1 addition & 1 deletion docs/python-tutorial/chapters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in-depth Python features.
- [Brief Python History](01-python-history.md)
- [Introduction to Variables](02-intro-to-variables.md)
- [Data Types pt 1 (`string`, `int`, `float` & `bool`)](03-data-types-pt01.md)
- Builtin Functions pt 1 (`print` & `input` and type casting)
- [Builtin Functions pt 1 (`print` & `input` and type casting)](04-built-in-functions-pt-1.md)
- String Interpolation & Concatenation (f-strings, str.format & +)
- Operators pt 1 (Comparison & Arithmetic)
- Conditional Statements (`if`, `else` & `elif`)
Expand Down
1 change: 1 addition & 0 deletions zensical.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ nav = [
"python-tutorial/chapters/01-python-history.md",
"python-tutorial/chapters/02-intro-to-variables.md",
"python-tutorial/chapters/03-data-types-pt01.md",
"python-tutorial/chapters/04-built-in-functions-pt-1.md",
] },
{ "Resources" = [
"resources/index.md",
Expand Down