Creating Functions and Importing Modules - Python Programming for Beginners (2015)

Python Programming for Beginners (2015)

Chapter Seven: Creating Functions and Importing Modules

In this chapter, you will learn:

· What functions are and how to create them

· What modules are and how to import them into your program

In previous chapters we have used functions but not explained what they are. Functions are basically pieces of code which have already been written, meaning that you don't need to rewrite the full piece of code every time you want to do something.

Python has many pre-written functions ready for you which are saved in Modules that you can add into your programs. You can find many modules available for you online, which can speed up the development in your next program.

Let's start by explaining how to importing new modules.

Importing New Modules

Importing modules is incredibly easy. You simply use the following statement:

import module_name

So, for example, if you want to use the date and time in Python you need to import the datetime module. You do that with the following statement:

import datetime

Once you have imported datetime you can use all of the functions from the datetime module. You can use the date function from the datetime module in the following way:

datetime.date (2015, 9, 21)

The function above sets the date in the program to the 21st of September 2015. You can save yourself a lot of typing by importing the datetime module in the following way:

import datetime as d

This means that you can call any function from the datetime module by using d rather than datetime. For example:

d.date (2015, 9, 21)

If you want to create your modules then all you need to do is write the functions (we'll teach that in the next section). You then save the file as a .py file and save it in the same folder as the program you want to use it in. If you download a module you have found online, you also save it in the same folder as the program you want to use it in.

You then import it in the same way that you import any other module. If you have saved a module called checknumber.py then you would import it by typing out:

import checknumber

Creating Your Own Functions

Creating functions in Python is called “defining functions” and it isn't difficult to do. You define a function in the following way:

def function_name(parameters)

· code

· return[expression]

Once you have defined the function you type the code out and end the function with the word return.

Here is an example of a function:

def check_if_true(number_check)

· if number_check == 5:

o return true

· else:

o return false

To use this function you could use the following code:

check_number = input(“Enter a number”)

answer = number_check(check_number)

· print answer

If you run the code then you will find that if you type the number 5 it will display “true” and if not it displays “false”. This works because the return statement sends true or false back to the variable “answer”.

It is important to recognise that any variables declared inside a function can only be used inside the function. These are called local variables. Variables declared outside of functions can be used inside of a function as well, these functions are called global variables.

You can declare a variable in a function with the same name as a global variable, but it isn't recommended as it can get confusing. If you do declare a variable outside and inside of a function with the same name then the only variable used inside the function will be the local variable.


Best Practices & Common Mistakes

Do’s

· Use Comments

It is important to use comments as much as possible in your work. Get into the habit right from the beginning. Even though your smaller projects may not require much editing, it will really come in handy when you are creating big projects.

· Stick to Your Naming Conventions

Name your variables in order with your chosen naming convention. It helps you to avoid silly mistakes. Again, this might not seem important when you are creating small programs. But when you start creating big programs you need to do whatever you can do to help avoid mistakes.

· Save Your Program Regularly

I have once written hundreds of lines of code without saving and lost it all with a computer crash. I can't tell you how sick I felt afterwards. Don't make my mistake! Save your program as often as possible and you won't risk losing it.

· Keep a Backup

Make a backup of your program and before you make any big changes to your program, save it to your backup. Sometimes your changes won't work very well and they may even cause awful bugs in your program. In those cases you will be relieved to be able to go back to the way it was before you made those changes.

Don’ts

· Forget to Place a : On If Statements

One big mistake most newbies to forget to place a : at the end of the line on if statements. If you're getting errors and you don't know what they are, make sure you've remembered to place a : at the end of the line.

· Use The Wrong Variable Scope

Remember that any variable declared in a function can only be used in the function. If you try to use it outside of the function it won't work.

· Cause an Infinite Loop!

If you're using a while loop, don't forget to make sure that the loop can end at some point!


Conclusion

Well done for working your way through this book and learning how to program in Python. You have learned everything you need to learn in this book to start programming in Python. In fact, you have already created many programs.

Now that you have learned how to create computer programs in Python you can start your journey into the wonderful world of computer programming. Use this book as a reference whenever you get stuck with anything, which you might find you do moving forward.

Now that you have all of the basics of Python you can start creating your own programs. I'm excited to see what you can produce – and you should be very excited too!