Book Image

Python Fundamentals

By : Ryan Marvin, Mark Ng’ang’a, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Ng’ang’a, Amos Omondi

Overview of this book

<p>After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions.</p> <p>As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation.</p> <p>By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.</p>
Table of Contents (12 chapters)
Python Fundamentals
Preface

Chapter 3: Control Statements


Activity 14: Working with the if Statement

Solution:

answer = input("Return TRUE or FALSE: Python was released in 1991:\n")

if answer == "TRUE":
  print('Correct')
elif answer == "FALSE":
  print('Wrong')
elif answer != ("TRUE" or "FALSE"):
  print('Please answer TRUE or FALSE')

print('Bye!')

Activity 15: Working with the while Statement

Solution:

  1. Define the password and the Boolean validator first:

    user_pass = "random"
    valid = False
  2. Initiate the while loop and ask for the user's input:

    while not valid:
         password = input("please enter your password: ")
  3. Validate the password and return an error if the input is invalid:

         if password == user_pass:
           print("Welcome back user!")
           valid =  True
         else:
           print("invalid password, try again... ")

    Your block of code should look as follows; take note of the indentation:

    user_pass = "random"
    valid = False
    while not valid:
         password = input("please enter your password: ")
         if password == user_pass:
           print("Welcome back user!")
           valid =  True
         else:
           print("invalid password, try again... ")

Activity 16: The for loop and range Function

Solution:

>>> total = 0
>>> for number in range(2,101,2):
    total += number
>>> print(total)

Activity 17: Nested Loops

Solution:

for even in range(2,11,2):
    for odd in range(1,11,2):
        val = even + odd
        print(even, "+", odd, "=", val)

Activity 18: Breaking out of Loops

Solution:

for number in range(0,200): 
    if number == 0:
        continue
    elif number % 3 != 0:
        continue
    elif type(number) != int:
        continue
    else:
        pass
    print(number)