Skip to content
Open
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 numberDiff line numberDiff 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this you can

Usually, you add a comma after an introductory phrase like 'with this'.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot a space after "commas"? Like that you added the comma after the brackets, though; well done.

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 can NOT be used to combine different strings or

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, though "can not" is also fine, "cannot" is often recommended. (For emphasis, one could use uppercase letters for the entire word "cannot".)

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you want to use …

A comma after the transition 'so' would be preferred.

`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 it casts to. Here is the list of type-casting functions with the type they cast to:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are named the same as the data type it casts to

Since we are using a plural pronoun "they", it would be more suitable to use "they cast" instead of "it casts" afterwards.


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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to get a number from the user you can do this

A comma before "you can do" would be preferred. Alternatively, the different parts can be switched like so:

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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