Lesson 1: Introduction to Python 3

by Dr Derek Peacock 9th Nov 2017

This lesson will focus on:-

1.print()
2.input()
3.strings
4.integer numbers
5.floating point numbers

Program Input and Output (shift + enter to execute the cell)

In [3]:
"hello"
Out[3]:
'hello'
In [4]:
123
Out[4]:
123
In [5]:
1 + 2
Out[5]:
3

Unrecognised Words/Names

In [12]:
hello
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-b1946ac92492> in <module>()
----> 1 hello

NameError: name 'hello' is not defined

Printing numeric and string values

In [7]:
print("Hello World!")
Hello World!
In [8]:
print(33 * 44)
1452
In [11]:
print("33 * 44 = ", 33 * 44)
33 * 44 =  1452
In [13]:
print("33" + "44")
3344

Getting user input

In [14]:
first_name = input("Please enter your name: ")
print("Hello, " + first_name)
Please enter your name: Derek
Hello, Derek

Using Numbers

you need to use built in conversion functions such as int(), float() or str()

In [16]:
# Add Two Numbers Program
# This program will caculate the sum of two numbers
#
# by Derek Peacock

print("Please enter two numbers")

first_number = int(input("First Number:"))
second_number = int(input("Second Number: "))

sum = first_number + second_number

print()
print("The sum of the numbers = ", sum)
print()
Please enter two numbers
First Number:3
Second Number: 4

The sum of the numbers =  7

In [53]:
# VAT Calculator
# This program will calculate the total cost of goods including VAT
#
# by Derek Peacock

cost = input("Please enter the cost without VAT: £")
cost = float(cost)

total_cost = float(cost) * 1.20

print("The total cost is = £", total_cost)
print()
Please enter the cost without VAT: £100
The total cost is = £ 120.0

Print Formatting

this example shows how to format numerical output. It is not easy to get the euro symbol as it is not available on the keyboard.

In [52]:
# Euro Calculator
# This program will convert British Pounds to Euros
#
# by Derek Peacock

print("-" * 60)
print("\tDerek's Euro Calculator")
print("-" * 60, "\n\n\n")

amount = input("Please enter the amount in British Pounds: £")
print()
amount = float(amount)

euros = amount / 0.8838

# Two ways of printing in a particular format

print("You will get £{:0,.2f} euros for £{:0,.2f}".format(euros, amount))
print("You will get %1.2f for £ %1.2f " %(euros, amount))
------------------------------------------------------------
	Derek's Euro Calculator
------------------------------------------------------------ 



Please enter the amount in British Pounds: 12345
You will get £13,968.09 euros for £12,345.00
You will get 13968.09 for £ 12345.00