Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Added Chapter 4 of Level 1 of Python Tutorial#11
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
base:feature/python-tutorial
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7391354368f8ae56b0c497e11ebd9a6f4d7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A comma before "you can do" would be preferred. Alternatively, the different parts can be switched like so:
| ||
| ```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. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, you add a comma after an introductory phrase like 'with this'.