There are many useful functions that have already been created and are ready to be used. Learn how to use a couple of important built-in functions.
- [Instructor] Python has some functions that have already been defined. These are called built-in functions, and they are available for you to use anytime. A handy built-in function is print, and in programming printing means, showing something on the screen. So the print function in Python takes in data and prints out the data so that it's displayed on the screen. A cool thing about the print function is that it works with any type of data. For example, if I wanted to print out the phrase hello world, I would write print ("Hello World"). And now I'm going to run the cell by hitting Shift + Enter. There, I just printed out a string. Let's look at another example. If I wanted to display the result of 2,000 plus 19 on the screen, I would write print (2,000 + 19). And now I'm going to run by hitting Shift + Enter. There it is, the computer performed the arithmetic operation 2,000 plus 19 resulting in the integer 2019 and printed that out. Another handy built-in function is input. The input function allows you to ask the user for data and receive that data for you to work with. When you use the input function, you want to specify a prompt that will be displayed to the user as well as create a variable that will store the user's typed response to the prompt. Now I'm going to walk through an example of using the input function. I might do it like this: name = input, and I type in the prompt what is your name? And on the next line I type in color = input and I type in the prompt what is your favorite color? Once I run this I can act as the user. So I'm going to go ahead and run this cell now. Each time a prompt pops up, I can type my response in the little blue box and press Enter. So the first question I'm asked is what is your name. So I'm going to type in my name and hit Enter. And the next question I'm asked is what is your favorite color. So I go ahead and answer that question and hit Enter. Now that the user has provided data by responding to these prompts, I want to check if I received the data. In other words I want to check if the variable's name and color are actually storing the user's name and the user's favorite color respectively. I can do this by printing out name and color. So in this cell here, I'll print (name). And on the next cell, I'll print (color). And there you go, name matches the user's name and color matches the user's favorite color. Now that I've confirmed that I did receive the user's data, I can do whatever I want with it. For example, I can greet the user and respond to them about their favorite color. That would look something like this: print parentheses, and inside I can write "Hi" + name + ! + color + is a great color. And I can hit run now. These plus signs are addition operators in programming and I used them here to join the individual strings together to create one large string. This is also known as string concatenation. Now this is a bit more advanced for the purposes of this course, but this is a quick preview of how powerful functions can be. So now that you know what built-in functions are and how to use print and input, try using them yourself. It's easy to do, and these are a couple of Python's most powerful built-in functions.
This course was created by Madecraft. We are pleased to host this content in our library.
- What is Python?
- Strengths and benefits of Python
- Types of data
- Storing data using variables
- Built-in and custom functions
- Lists and tuples
- Conditional statements
- Iteration with loops
- Recursive functions
- Object-oriented programming