We learned how to declare variables and using them, one of values that could be assigned to the variables could be a class, but we didn't dug up more on classes so it's time we do it now.
Functions:
A function is used to create a set of codes that can be called (executed) when needed and all the times needed.
To create a function we use def followed by the name of the function:
Code: Select all
def hello():
print "Hello World"Code: Select all
hello()Code: Select all
def hello(text):
print textCode: Select all
hello(text="Hello World")Code: Select all
hello("Hello World")Code: Select all
hello("Hello World")
hello("Hola Mundo")
hello("Goodbye World")Long story short: a class is used to create an object.
An object is a class with methods (functions) and attributes:
Code: Select all
class hello:
def hello(self, number)
self.number = number
print "Hello World\n" * numberCode: Select all
hello().hello(3)Code: Select all
Hello World
Hello World
Hello WorldTo create a class and inherit from another class we use pass:
Code: Select all
class hello:
def hello(self, number)
self.number = number
print "Hello World\n" * number
class world(hello):
passCode: Select all
world().hello(3)As you already should know, variables can also be classes:
Code: Select all
x = world()
x.hello(3)One thing that Python has is what they call "batteries included", that is a lot useful modules for varying uses.
To use a module one must first import it:
Code: Select all
import randomCode: Select all
random.randrange(1234567890)Previous: viewtopic.php?f=37&t=11885
Next: viewtopic.php?f=37&t=11920
Advertising