משתמש:Yoelpiccolo31/פיתון

<syntaxhighlight lang="python">

text in Python

</syntaxhighlight>

# A first Python script- page 107 

import sys                  

print(sys.platform) 

print(2 ** 100)            

x = 'Spam!' 

print(x * 8)

מחלקות ועצמים בפיתון עריכה

Taken (with my notes and additions) from the site: [1].
# == A very basic class: ==

class MyClass:  # inside the class are defined a variable and a function.
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass() # MyClass is an object of the class Myclass above.
# object    # of the class above: MyClass.
            # The object "myobjectx" is related to the class,
            # therefore it contains all its characteristics:
            # a variable (line 4) ,
            # and a function (lines 6-7).


# "myobjectx" is an object of the class "MyClass".
# It contains the variable and the function
# defined within the class called "MyClass".

# == Accessing Object Variables ==

# To access the variable inside of the newly created object "myobjectx"
# you would do the following:

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()

myobjectx.variable
# accessing the variable inside the object "myobjectx".
# this object is related to the class "MyClass",
# therefore contains a variable and a function
# (We decided to define the class "MyClass",
# that it would contain a function and a variable).

# == Outputing the string "blah" ==

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()

print(myobjectx.variable)

# we just added the instruction "print" to line 34.
# meaning: print the content of the variable
# defined in the object "myobjectx", which is of the class "MyClass",
# therefore this object contains all of its content:
# a variable and a function.

# == create multiple different objects that are of the same class ==

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

# creating two different objects: "myobjectx", "myobjecty" of the same class: MyClass().

myobjectx = MyClass()
myobjecty = MyClass()

# changing the initial content of the variable in the object "myobjecty",
# which is of the class "MyClass".

myobjecty.variable = "yackity"

# Then print out both values
print(myobjectx.variable) # output: blah
print(myobjecty.variable) # output: yackity

# == Accessing Object Functions ==

# Made the same way as accessing the variable inside the object, of the class "MyClass".

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()
# object    # of the class "MyClass".

myobjectx.function()
# accessing the function inside "myobjectx" object, of the class "MyClass".

# == Exercise ==

# We have a class defined for vehicles.
# Create two new vehicles called car1 and car2.
# Set car1 to be a red convertible worth $60,000.00 with a name of Fer,
# and car2 to be a blue van named Jump worth $10,000.00.

# define the Vehicle class
class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str

# your code goes here:

# 1. Creating two objects: "car1" and "car2".

car1     =     Vehicle()
# object1      # class

car2     =     Vehicle()
# object2      # the same class

# 2. changing initial content of the above defined objects, car1 and car2, of "Vehicle" class.

car1.color = "red"
car1.name = "Fer"
car1.value = 60000.00

car2.kind = "Van"
car2.color = "blue"
car2.name = "Jump"
car2.value = 10000.00

# test code
print(car1.description())
print(car2.description())


# == Result ==

blah
blah
yackity
This is a message inside the class.
Fer is a red car worth $60000.00.
Jump is a blue Van worth $10000.00.