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

Recommended Answers

All 2 Replies

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.

Awesome mate, thats exactly what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.