0

So this marks my very first forum page on this great database of information named DANIWEB.

So just quickly, as i am still in school and learning python, I really need to know, just an explination on:

The def statement, what you can do with it and how to use it?

and:

The import statement, what you can do with it, how to use it and what you can import.

As a school project we are creating a text based game, a lot like Zork. I have to say we are going well and I have coined a lot of ideas of here. Any help will be appreciated

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by Blacktono4
0

The keyword def is used to define functions (and methods, when you start studying classes and objects). It essentially allows you to give a name to a piece of code, so that you can use it over and over. For example, this is a one line function, followed by a for: loop that calls it five times:

def star():
   print '*',

for i in range(5):
    star()

Functions are also used to break the program up into easy to understand pieces. You can pass information to a function by passing it arguments, and the function can return a result. For example, this function calculates the area of a cube:

def cubeArea(size):
    return (size ** 2) * 6   # the area of each side, times the number of sides 

area = cubeArea(4)           # area should now equal 96

The import keyword is used to get functions and constants from other files of source code, called modules. Python includes a large library of modules which you can import, and you can import from a module you've written yourself into another module.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.