So today’s lesson is all about functions. What is a function anyway? Functions are blocks of code that do specific actions on the data you give them. Kind of like mathematical functions, they take in an input or inputs, and give you an output. Python functions can either return to you some new data or change the data you put into them.

Alright, lets dive into some basic Python functions.

The print() Function

The first, and probably most useful, function is the print() function. This function takes any data you give it and basically writes it down.

To show you an easy example, lets open up idle.

Create a variable named name and assign to it a string containing your name. On the next line, enter the following and then run the program.I’ve provided line numbers to help, don’t actually write them in.

  1. name = “Trey”
  2. print(“Hello, ” + name)

Hello computer! Take a second to appreciate the fact that you just wrote your first program in Python! This is the first step, on an extremely rewarding journey to writing all kinds of Python programs.

Anyway, the print() function can either print out variables, or raw data that you put in. Just be careful, you can’t mix the type of data you put into the print() function.

Lets do another example, lets say I want to write a program that will add two numbers. Lets go with 5 and 11. Here’s an example of that code.

  1. num_one = 5
  2. num_two = 11
  3. sum = num_one + num_two
  4. print(sum)

If you run this code it will give you 16, but lets rethink it a little bit.

The input() Function

Instead of putting in two numbers ourselves, what if we could ask the user to put in any two numbers.

We can do that with the input() function. This function takes input from the user and stores it as a string, wherever the input function was.

For example, we can rewrite our code like this to ask the user for two numbers

  1. num_one = input()
  2. num_two = input()
  3. sum = num_one + num_two
  4. print(sum)

To help out the user , lets add a helpful prompt to let them know what exactly the program is asking of them. We can do that by putting a string in between the parentheses of the input() function

  1. num_one = input(“Enter a number: “)
  2. num_two = input(“Enter another number: “)
  3. sum = num_one + num_two
  4. print(sum)

Now lets run the program. Hmm, that’s odd. The problem is the input function only returns a string to you when you call it. So instead of adding the two numbers, the program is just writing the numbers down next to each other. This is cool, but far from what we wanted.

To fix this  we have to introduce a new function.

The int() Function

The code we have now is cool, but we still want it to add the two numbers. To do this, we have to change the strings given to us by the input() function to actual numbers. Thats exactly what the int() function does.

The int() function will change a string into a number, given that the string actually contains a number. This is extremely useful when collecting information from the user. To fix our code we can add these new lines

  1. num_one = input(“Enter a number: “)
  2. num_two = input(“Enter another number: “)
  3. num_one = int(num_one)
  4. num_two = int(num_two)
  5. sum = num_one + num_two
  6. print(sum)

As you can see num_one and num_two are reassigned to the number versions of themselves. However, this solution adds two new lines to our code. To save some room, we can use both the int() and input() functions in one line

  1. num_one = int(input(“Enter a number: “))
  2. num_two =int(input(“Enter another number: “))
  3. sum = num_one + num_two
  4. print(sum)

When you combine functions like this, be careful to pair up your parentheses. If you don’t, your program will most definitely crash.

Now our program is looking pretty good, but lets try one more thing. Enter 3.3 in as the first number.

It appears as though we have a problem. Our code works fine for whole, numbers, but is doesn’t allow for decimals. I will get into exactly how to read that error message at a later date.

It looks like we’re going to need another function to fix this.

The float() Function

This situation calls for the float() function. It does the same thing as the int() function, only it changes strings to floats. Lets use the float() function instead of the int() function for this line of code.

  1. num_one = float(input(“Enter a number: “))
  2. num_two =float(input(“Enter another number: “))
  3. sum = num_one + num_two
  4. print(sum)

Now we get our desired output, no matter what number the user puts in.

And thats pretty much it! I hope you guys found that helpful. If you’re following along at home, I recommend that you create a folder for these lessons where you can store these little programs. Not only are we going to revisit them, they’re great for tracking you progress as well.

Alrighty then, I’ll see you guys in the next one.

  1. Hey Trey! I got your email, so I figured I’d drop a comment. The only real advice I can give is just stick to it. I’ve been blogging for about a year, and I haven’t been that successful. Your writing style is great, so you shouldn’t have any problems catching up to me. I’m excited to see what else you have to say! 🙂

Leave a Reply