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 1: Introducing Python


Activity 1: Working with the Python Shell

Solution:

>>> print("Happy birthday") 
Happy birthday 
>>> 17 + 35 * 2 
87 
>>> print(1, 2, 3, 4, 5, 6, 7, 8, 9) 
1 2 3 4 5 6 7 8 9
>>> 

Activity 2: Running Simple Python Scripts

Solution:

import sys 

print("*---------------------------------") 
print("|", "First name: ", sys.argv[1]) 
print("|", "Second name: ", sys.argv[2]) 
print("*---------------------------------") 

Activity 3: Using Variables and Assign Statements

Solution:

  1. Open your editor.

  2. Create a file named calculate_speed.py and save it.

  3. On the first two lines, we'll declare our two variables for the distance in kilometers and the time in hours:

    distance_in_km = 150
    time_in_hours = 2
  4. In the next two lines, we'll calculate the distances in miles and the distance in meters based on the distance in kilometers:

    distance_in_mi = distance_in_km / 1.6
    distance_in_mtrs = distance_in_km * 1000

    We'll then calculate the time in seconds based on the time in hours:

    time_in_seconds = time_in_hours * 3600
  5. Next, we'll calculate the speed in kilometers per hour, the speed in miles per hour, and the speed in miles per second:

    speed_in_kph = distance_in_km / time_in_hours
    speed_in_mph = distance_in_mi / time_in_hours
    speed_in_mps = distance_in_mtrs / time_in_seconds
  6. Finally, we'll print out our results:

    print("The speed in kilometers per hour is:", speed_in_kph)
    print("The speed in miles per hour is:", speed_in_mph)
    print("The speed in meters per second is:", speed_in_mps)
  7. We can now save our script and run it by using the python calculate_speed.py command.

Activity 4: Variable Assignment and Variable Naming Conventions

Solution:

  1. Open your editor.

  2. Create a file named circle.py and save it.

  3. On the first two lines, we'll declare our constant, π (PI), and the radius of the circle:

    PI = 3.14159
    radius = 7
  4. On the next two lines, we'll run our calculations:

    area = PI * radius**2
    circumference = 2 * PI * radius

    The ** operator raises the value to the specified exponent; in this case, the exponent is 2.

  5. Lastly, we'll display the results:

    print("Circumference of the circle:", circumference)
    print("Area of the circle:", area)
  6. We can now save our script and run it by using the python circle.py command.

Activity 5: Fixing Indentations in a Code Block

Solution:

>>> if 5 > 2:
...     print("Greater than")
...     x = 5
...     print(x * 2) 
... else:
...     print("Less than")
...     print(2)

Activity 6: Implementing User Input and Comments in a Script

Solution:

  1. Open your editor.

  2. Create a file named multiplication_table.py and save it.

  3. On the first line, we'll add a docstring explaining what our file does. Then, we'll assign number to the user's input and cast it to an integer:

    """
    This script generates a multiplication table from 1 to 10 for
    any given number.
    """
    number = int(input("Generate a multiplication table for: "))
  4. Next, we'll print 20 underscores as the top border of the table:

    print("_" * 20)
  5. We'll then multiply our number with each number from 1 to 10 and print that out:

    print("1:", number)
    print("2:", number * 2)
    print("3:", number * 3)
    print("4:", number * 4)
    print("5:", number * 5)
    print("6:", number * 6)
    print("7:", number * 7)
    print("8:", number * 8)
    print("9:", number * 9)
    print("10:", number * 10)
  6. Finally, we'll print 20 underscores again for the bottom border of the table, as in step 4.

    print("_" * 20)
  7. Save the file and run it by using the python multiplication_table.py command.