<kaggle_start><code># ### This file was made by me while I was teaching myself Python and I would be happy if this can help someone on Kaggle who wants to learn Python.
# # PYTHON PROGRAMMING FUNDAMENTALS
# ## CONDITIONS AND BRANCHING
# ### Comparison Operators:
a = 5
print(a == 6)  # in this case '==' determines wether the two values are equal or not
print(a == 5)
print(a > 7)  # checking for one value greater than the other
print(a > 4)
print(a >= 5)  # greater than or equal to
print(a < 4)  # checking for one value less than the other
print(a < 8)
print(a <= 5)  # less than or equal to
print(a != 8)  # not equal to
print(a != 5)
print("salman" == "khan")  # can compare strings too
print("salman" != "khan")
print("khan" == "khan")
print("A" == "a")
print("A" > "B")  # actually comparing ascii values here
print("B" > "A")

# ### Branching:
# #### The IF Statement:
age = 21
if age >= 18:  # if this statement qualifies the person can enter
    print("You can enter,")
print(
    "move on!"
)  # this statement will run no matter what, since it is out of the if block
age = 12
if age >= 18:
    print("You can enter,")
print("move on!")

# #### The ELSE Statement:
age = 13
if age >= 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
else:  # if the above statement doesn't qualify, the person will be suggested another place
    print("Go see Meat Loaf,")
print("move on!")
age = 31
if age >= 18:
    print("You can enter,")
else:
    print("Go see Meat Loaf,")
print("move on!")

# #### The ELIF Statement:
age = 13
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")
age = 31
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")
age = 18
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")

# ### Logic Operators:
# NOT operator inverses the boolean value
print(not True)
print(not False)
# OR operator comes in between multiple boolean values and will be resulted in single
# for the resulted boolean value to be true, any one of those multiple values must be a true
# for the resulted boolean value to be false, all of the input multiple values must be false
print(True or True)
print(True or False)
print(False or True)
print(False or False)  # here both input values are false, hence reslted in false
# can be used in the if statements like below!
movie_release_year = 1985
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
movie_release_year = 2008
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
movie_release_year = 2015
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
# AND operator comes in between multiple boolean values and will be resulted in single (just like OR)
# for the resulted boolean value to be true, all of the multiple values must be a true
# for the resulted boolean value to be false, any one of the input multiple values must be false
print(True and True)  # all are true
print(True and False)
print(False and True)
print(False and False)
birth_year = 1990
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")
birth_year = 2000
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")
birth_year = 1973
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")

# ## LOOPS
# range generates an ordered sequences
# if its provided with one integer, it will range from zero to a number 1 less than that number
# if its provided with two integers, it will range from first to a number 1 less than second integer
print(range(3))
print(range(10, 34))
# we can generate a list from range
print(list(range(3)))
print(list(range(10, 23)))

# ### FOR Loops:
# here in this example, we have 5 different colored squares, we want to paint all 5 squares with white
# we will use loops here
# for loop is used here 5 time, because we want to paint each square one by one
# with each turn of loop, one square is painted white and the program will rum 5 times
# these 5 times are achieved by the range provided by us 'range(0, 5)'
# this range will also serve as the index of list
squares = ["red", "yellow", "green", "purple", "blue"]
for i in range(0, 5):
    squares[i] = "white"
print(squares)
squares = ["red", "yellow", "green", "purple", "blue"]
for square in squares:
    print(square)
    # square = "white" will also do the work done in the above cell
    # as square here will one-by-one call the items of the list i.e. "red", "yellow", etc
    # no need to use indexes this way
    # no need to manually give the range too, as it will automatically run for all of the items of list
# enumerate is the function that is used to obtain the index and value of the sequence
boxes = ("red", "yellow", "green")
for i, box in enumerate(boxes):
    print(i, box)

# ### WHILE Loops:
# here we have a list of colors, we want to pick only reds from here
# while loop will work until we are getting reds
# loop will break once we would encounter a color that is not red and we will be then out of the loop
colors = ["red", "red", "red", "yellow", "green", "red", "orange", "green", "red"]
red = []
i = 0
while colors[i] == "red":
    red.append(colors[i])
    i += 1
print(red)

# ## FUNCTIONS
# ### Built in Functions
# len() funtion takes a sequence and tells length of that sequence
# can be used on strings, lists, sets and dictionaries
print(len("salman"))
print(len([2, 1, 0, 10, 45]))
# sum() fuction takes numbers in sequence and returns their total sum
# can find sums for integers, floats and complex numbers
print(sum([2 + 5j, 9 + 13j]))
print(sum([2, 1, 0, 10, 45]))
print(sum((3.7, 67)))

# #### SORTED VS SORT
list_forsort = [123, 1200, 24, 6, 28, 12]  # example list
# sorted() sorts the list in ascending order, if haven't instructed otherwise
# generates another list, does not alter the original list
print(sorted(list_forsort))
print(list_forsort)
# sort is a function that will alter the orginal list as it does not generate another list
print("Initial list:", list_forsort)
list_forsort.sort()
print("After 'sort()' function:", list_forsort)


# ### Making Functions
def give_double(x):  # how to define function
    x_doubled = 2 * x  # function body
    return x_doubled  # return value


print(give_double(34))  # calling a function
doubled_4 = give_double(4)  # assigning return of function to a variable
print(doubled_4)


# Statement in triple double/single quotes is Documentation String, helps to document the purpose of the function
def squared(a):
    """
    squares the given number"
    """
    b = a**2
    return b


c = squared(4)
print(c)
help(squared)


# Multiple Parameter, a function can have more than 1 arguments
def product(x, y):
    z = x * y
    return z


print(product(23, 56))


def product(w, x, y):
    z = w * x * y
    return z


print(product(12, 56.87, 5))


# it is not a must for a function to return a value
# there can be an equally workable and normal function with no return value
def SK():
    print("Salman")
    print("Khan")


SK()


def no_work():
    pass  # Python doesnt allow a function to have no body, here pass will fill in that requirement and will be doing nothing


print(no_work())  # when we dont return a vlue for a function, python saves a None in it


def no_work_none():  # Equilvalent to the above function
    return None


print(no_work_none())


def add1(a):
    b = a + 1
    print(a, "+ 1 =", b)
    return b


add1(34)
a_list = [10, 9, 8, 7, 6, 5]


def printStuff(Stuff):
    for i, s in enumerate(Stuff):  # using a loop inside a function
        print("Album", i, "Rating is", s)


printStuff(a_list)


def collectnames(
    *name,
):  # if we put * before argument, the function collects the argument
    for name in name:
        print(name)


collectnames(
    "salman", "khan", "m"
)  # no matter how much names, we put in the argument, the function will print it


def show_dict(**kv):  # same can be applied on dictionaries as well
    for key in kv:
        print(key, kv[key])


show_dict(name="salman", age=26, city="karachi")


# #### Global and Local Variables
# variables defined outside the function are called gloabal variables
def AddDC(x):
    x = (
        x + "DC"
    )  # we have called a variable that hasnt been asssinged yet inside of function as well
    print(x)
    return x


x = "AC"  # we are assigning the variable here
z = AddDC(x)


# local variables only exsists only within the scope of the function
def Thriller():
    Date = "1897"
    return Date


Date = "1982"
print(Thriller())
print(Date)


def PinkFloyd():
    global ClaimedSales  # a way how we can create a global variable inside a function
    ClaimedSales = "45M"
    return ClaimedSales


PinkFloyd()
print(ClaimedSales)


# ## OBJECTS AND CLASSES
# importing the library, for later use
# library not explained now
# we will just use this library for methods on objects
import matplotlib.pyplot as plt


class Circle(object):  # how to create a class
    def __init__(self, radius, color):
        """
        __init__ is a special method or constructor to initialize data attributes
        self defines an object and is unique for each object
        radius and color in this case are the attributes of the objects that will be created in this class
        """
        self.radius = radius
        self.color = color

    def add_radius(
        self, radius_inc
    ):  # creating a fuction for the class, to modify the object's radius
        self.radius += radius_inc
        return self.radius

    def reduce_radius(self, radius_dec):
        self.radius -= radius_dec
        return self.radius

    def draw_circle(self):
        """
        using matplotlib to draw the circle
        just copied the body of this function for now
        this will somehow print the drawn circle on screen
        """
        plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))
        plt.axis("scaled")
        return plt.show


class Rectangle(object):
    def __init__(self, height, width, color):
        self.height = height
        self.width = width
        self.color - color


cr10 = Circle(10, "red")  # creating a new object in class "Circle"
cb4 = Circle(4, "blue")  # creating another
print(cr10.color)
print(cb4.radius)
print(cr10.draw_circle())
cb4.radius = 40  # changing the attribute
print(cb4.radius)
cb4.add_radius(-36)  # calling method to increase the radius of the cirlce
print(cb4.radius)
cb4.reduce_radius(2)  # calling method to reduce the radius of the cirlce
print(cb4.radius)
dir(cb4)  # helps getting a list of methods that we can apply on a certain object
<|endoftext|>
[{"Id": 63841560, "ScriptId": 17200474, "ParentScriptVersionId": NaN, "ScriptLanguageId": 9, "AuthorUserId": 7112435, "CreationDate": "05/24/2021 21:45:31", "VersionNumber": 1.0, "Title": "Python Conditions, Loops and Functions", "EvaluationDate": "05/24/2021", "IsChange": true, "TotalLines": 406.0, "LinesInsertedFromPrevious": 406.0, "LinesChangedFromPrevious": 0.0, "LinesUnchangedFromPrevious": 0.0, "LinesInsertedFromFork": NaN, "LinesDeletedFromFork": NaN, "LinesChangedFromFork": NaN, "LinesUnchangedFromFork": NaN, "TotalVotes": 5}]
# ### This file was made by me while I was teaching myself Python and I would be happy if this can help someone on Kaggle who wants to learn Python.
# # PYTHON PROGRAMMING FUNDAMENTALS
# ## CONDITIONS AND BRANCHING
# ### Comparison Operators:
a = 5
print(a == 6)  # in this case '==' determines wether the two values are equal or not
print(a == 5)
print(a > 7)  # checking for one value greater than the other
print(a > 4)
print(a >= 5)  # greater than or equal to
print(a < 4)  # checking for one value less than the other
print(a < 8)
print(a <= 5)  # less than or equal to
print(a != 8)  # not equal to
print(a != 5)
print("salman" == "khan")  # can compare strings too
print("salman" != "khan")
print("khan" == "khan")
print("A" == "a")
print("A" > "B")  # actually comparing ascii values here
print("B" > "A")

# ### Branching:
# #### The IF Statement:
age = 21
if age >= 18:  # if this statement qualifies the person can enter
    print("You can enter,")
print(
    "move on!"
)  # this statement will run no matter what, since it is out of the if block
age = 12
if age >= 18:
    print("You can enter,")
print("move on!")

# #### The ELSE Statement:
age = 13
if age >= 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
else:  # if the above statement doesn't qualify, the person will be suggested another place
    print("Go see Meat Loaf,")
print("move on!")
age = 31
if age >= 18:
    print("You can enter,")
else:
    print("Go see Meat Loaf,")
print("move on!")

# #### The ELIF Statement:
age = 13
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")
age = 31
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")
age = 18
if age > 18:  # if this statement qualifies, the person can enter
    print("You can enter,")
# if the above one doesnt qualify, program will run this "ELIF" statement and do as it says on qualification
elif age == 18:
    print("Please visit Pink Floyd,")
else:  # if both of the above don't work, it will run this one
    print("Go see Meat Loaf,")
print("move on!")

# ### Logic Operators:
# NOT operator inverses the boolean value
print(not True)
print(not False)
# OR operator comes in between multiple boolean values and will be resulted in single
# for the resulted boolean value to be true, any one of those multiple values must be a true
# for the resulted boolean value to be false, all of the input multiple values must be false
print(True or True)
print(True or False)
print(False or True)
print(False or False)  # here both input values are false, hence reslted in false
# can be used in the if statements like below!
movie_release_year = 1985
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
movie_release_year = 2008
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
movie_release_year = 2015
if movie_release_year < 2000 or movie_release_year > 2010:
    print("Yeah! I have watched it!")
else:
    print("No! I have not seen any movie released in that decade!")
# AND operator comes in between multiple boolean values and will be resulted in single (just like OR)
# for the resulted boolean value to be true, all of the multiple values must be a true
# for the resulted boolean value to be false, any one of the input multiple values must be false
print(True and True)  # all are true
print(True and False)
print(False and True)
print(False and False)
birth_year = 1990
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")
birth_year = 2000
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")
birth_year = 1973
if birth_year >= 1981 and birth_year <= 1996:
    print("You're a Millennial!")
else:
    print("You're not a Millennial!")

# ## LOOPS
# range generates an ordered sequences
# if its provided with one integer, it will range from zero to a number 1 less than that number
# if its provided with two integers, it will range from first to a number 1 less than second integer
print(range(3))
print(range(10, 34))
# we can generate a list from range
print(list(range(3)))
print(list(range(10, 23)))

# ### FOR Loops:
# here in this example, we have 5 different colored squares, we want to paint all 5 squares with white
# we will use loops here
# for loop is used here 5 time, because we want to paint each square one by one
# with each turn of loop, one square is painted white and the program will rum 5 times
# these 5 times are achieved by the range provided by us 'range(0, 5)'
# this range will also serve as the index of list
squares = ["red", "yellow", "green", "purple", "blue"]
for i in range(0, 5):
    squares[i] = "white"
print(squares)
squares = ["red", "yellow", "green", "purple", "blue"]
for square in squares:
    print(square)
    # square = "white" will also do the work done in the above cell
    # as square here will one-by-one call the items of the list i.e. "red", "yellow", etc
    # no need to use indexes this way
    # no need to manually give the range too, as it will automatically run for all of the items of list
# enumerate is the function that is used to obtain the index and value of the sequence
boxes = ("red", "yellow", "green")
for i, box in enumerate(boxes):
    print(i, box)

# ### WHILE Loops:
# here we have a list of colors, we want to pick only reds from here
# while loop will work until we are getting reds
# loop will break once we would encounter a color that is not red and we will be then out of the loop
colors = ["red", "red", "red", "yellow", "green", "red", "orange", "green", "red"]
red = []
i = 0
while colors[i] == "red":
    red.append(colors[i])
    i += 1
print(red)

# ## FUNCTIONS
# ### Built in Functions
# len() funtion takes a sequence and tells length of that sequence
# can be used on strings, lists, sets and dictionaries
print(len("salman"))
print(len([2, 1, 0, 10, 45]))
# sum() fuction takes numbers in sequence and returns their total sum
# can find sums for integers, floats and complex numbers
print(sum([2 + 5j, 9 + 13j]))
print(sum([2, 1, 0, 10, 45]))
print(sum((3.7, 67)))

# #### SORTED VS SORT
list_forsort = [123, 1200, 24, 6, 28, 12]  # example list
# sorted() sorts the list in ascending order, if haven't instructed otherwise
# generates another list, does not alter the original list
print(sorted(list_forsort))
print(list_forsort)
# sort is a function that will alter the orginal list as it does not generate another list
print("Initial list:", list_forsort)
list_forsort.sort()
print("After 'sort()' function:", list_forsort)


# ### Making Functions
def give_double(x):  # how to define function
    x_doubled = 2 * x  # function body
    return x_doubled  # return value


print(give_double(34))  # calling a function
doubled_4 = give_double(4)  # assigning return of function to a variable
print(doubled_4)


# Statement in triple double/single quotes is Documentation String, helps to document the purpose of the function
def squared(a):
    """
    squares the given number"
    """
    b = a**2
    return b


c = squared(4)
print(c)
help(squared)


# Multiple Parameter, a function can have more than 1 arguments
def product(x, y):
    z = x * y
    return z


print(product(23, 56))


def product(w, x, y):
    z = w * x * y
    return z


print(product(12, 56.87, 5))


# it is not a must for a function to return a value
# there can be an equally workable and normal function with no return value
def SK():
    print("Salman")
    print("Khan")


SK()


def no_work():
    pass  # Python doesnt allow a function to have no body, here pass will fill in that requirement and will be doing nothing


print(no_work())  # when we dont return a vlue for a function, python saves a None in it


def no_work_none():  # Equilvalent to the above function
    return None


print(no_work_none())


def add1(a):
    b = a + 1
    print(a, "+ 1 =", b)
    return b


add1(34)
a_list = [10, 9, 8, 7, 6, 5]


def printStuff(Stuff):
    for i, s in enumerate(Stuff):  # using a loop inside a function
        print("Album", i, "Rating is", s)


printStuff(a_list)


def collectnames(
    *name,
):  # if we put * before argument, the function collects the argument
    for name in name:
        print(name)


collectnames(
    "salman", "khan", "m"
)  # no matter how much names, we put in the argument, the function will print it


def show_dict(**kv):  # same can be applied on dictionaries as well
    for key in kv:
        print(key, kv[key])


show_dict(name="salman", age=26, city="karachi")


# #### Global and Local Variables
# variables defined outside the function are called gloabal variables
def AddDC(x):
    x = (
        x + "DC"
    )  # we have called a variable that hasnt been asssinged yet inside of function as well
    print(x)
    return x


x = "AC"  # we are assigning the variable here
z = AddDC(x)


# local variables only exsists only within the scope of the function
def Thriller():
    Date = "1897"
    return Date


Date = "1982"
print(Thriller())
print(Date)


def PinkFloyd():
    global ClaimedSales  # a way how we can create a global variable inside a function
    ClaimedSales = "45M"
    return ClaimedSales


PinkFloyd()
print(ClaimedSales)


# ## OBJECTS AND CLASSES
# importing the library, for later use
# library not explained now
# we will just use this library for methods on objects
import matplotlib.pyplot as plt


class Circle(object):  # how to create a class
    def __init__(self, radius, color):
        """
        __init__ is a special method or constructor to initialize data attributes
        self defines an object and is unique for each object
        radius and color in this case are the attributes of the objects that will be created in this class
        """
        self.radius = radius
        self.color = color

    def add_radius(
        self, radius_inc
    ):  # creating a fuction for the class, to modify the object's radius
        self.radius += radius_inc
        return self.radius

    def reduce_radius(self, radius_dec):
        self.radius -= radius_dec
        return self.radius

    def draw_circle(self):
        """
        using matplotlib to draw the circle
        just copied the body of this function for now
        this will somehow print the drawn circle on screen
        """
        plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))
        plt.axis("scaled")
        return plt.show


class Rectangle(object):
    def __init__(self, height, width, color):
        self.height = height
        self.width = width
        self.color - color


cr10 = Circle(10, "red")  # creating a new object in class "Circle"
cb4 = Circle(4, "blue")  # creating another
print(cr10.color)
print(cb4.radius)
print(cr10.draw_circle())
cb4.radius = 40  # changing the attribute
print(cb4.radius)
cb4.add_radius(-36)  # calling method to increase the radius of the cirlce
print(cb4.radius)
cb4.reduce_radius(2)  # calling method to reduce the radius of the cirlce
print(cb4.radius)
dir(cb4)  # helps getting a list of methods that we can apply on a certain object
