Skip to main content

Module 2: Write your own Python Programs


https://learn.microsoft.com/en-us/training/modules/python-create-run-program/

Introduction:


At some point you will need to build your own program in python that accepts user input and produces a result.

In this module, you are basically a brand new programmer who needs to write a series of utility programs for some senior staff.

You should learn:

  • Use functions to manage input and output to the console
  • Create variables to store data
  • Distinguish between data types
  • Use type conversion to convert between data types

A Python Program


You add a bunch of statements into a .py file and run it like this:

python your_program.py

The print() function continued


This is a complete function. You can call this anywhere. You need to use the () or it won't work.

Variables


This will store the equation inside a variable.

sum = 1 + 2 # 3
product = sum * 2
print(product)

Data Types


Each variable assumes a data type.
image.png

Some code as an example:

planets_in_solar_system = 8 # int, pluto used to be the 9th planet, but is too small
distance_to_alpha_centauri = 4.367 # float, lightyears
can_liftoff = True
shuttle_landed_on_the_moon = "Apollo 11" #string

How can you tell?

distance_to_alpha_centauri = 4.367 # looks like a float

Operators


You have a left side and a right side and an operator in between.

left_side = 10
right_side = 5
left_side / right_side # 2

Python uses two types of operators: arithmetic and assignment.

  • arithmetic opertators - +,-,*,/
  • assignment operators - =, +=, -=, /=, */

Dates


Use these for certain things within your application:

  • backup - filename created on...
  • condition - if Thursday, do x.
  • metric - 5gb yesterday, 10gb today

To use a date, import the date module:

from datetime import date
date.today()
# or
print(date.today())

Data type conversion


Lets say that you try to print out today's date in a sentence:

print("Today's date is: " + date.today())

Doesn't work. Why? You're trying to concatenate a string with a date.

You'll need to convert the date to a string by doing this:

print("Today's date is: " + str(date.today()))

image.png

Exercise 2


# Display today's date
from datetime import date
print(date.today())

# create a converter :)
parsecs = 11
lightyears = parsecs * 3.26
print(str(parsecs) + " parsecs is " + str(lightyears) + " lightyears")

Collecting input


You can pass things into the program through the command line, like this:

python3 backup.py 2020-05-13

You can then retrieve the arguments by using the sys module:

import sys

print(sys.argv)
print(sys.argv[0]) # program name
print(sys.argv[1]) # first arg

This is the result of the code:

['backup.py', '2023-01-01'] 
backup.py
2023-01-01

User Input


A great way to have input into the program is to have the user supply it via prompts.

print("Welcome to the greeter program")
name = input("Enter your name: ")
print("Greetings " + name)

Then, run the program

python3 input.py

And then enter in the prompts

Welcome to the greeter program
Enter your name:

Working with numbers


If you have a program that enters in numbers, you need to remember to convert from string to int.

print("calculator program")
first_number = input("first number: ")
second_number = input("second number: ")
print(first_number + second_number)

You then run the program and get the concatenation:

calculator program
first number: 3
second number: 4
34

Here's the correct code for that program:

print(int(first_number) + int(second_number))

Rerunning with the correct code gives us this:

calculator program
first number: 3
second number: 4
7

Exercise 3: Collecting Input


You want to rewrite the code to accept any input, rather than just hardcoding in the variables.

parsecs = 11
lightyears = parsecs * 3.26
print(str(parsecs) + " parsecs is " + str(lightyears) + " lightyears")

# becomes

parsecs_input = input("Input number of parsecs:")
parsecs = int(parsecs_input)
lightyears = 3.26156 * parsecs
print(parsecs_input + " parsecs is " + str(lightyears) + " lightyears")

Knowledge Check


image.png