Python Print Damian Gordon Your first Python program

  • Slides: 68
Download presentation
Python: Print Damian Gordon

Python: Print Damian Gordon

Your first Python program • When learning a new computer programming language, the first

Your first Python program • When learning a new computer programming language, the first thing typically taught is how to write a message to the screen saying “Hello, World”. • Let’s see how to do that:

print(“Hello, World”)

print(“Hello, World”)

# PROGRAM Hello. World. Program: print(“Hello, World”) # END.

# PROGRAM Hello. World. Program: print(“Hello, World”) # END.

# # Hello. World. Program – Version 1 A program to print out “Hello,

# # Hello. World. Program – Version 1 A program to print out “Hello, World” Written by: Damian Gordon Date: 10/09/2015 # PROGRAM Hello. World. Program: print(“Hello, World”) # END.

The PRINT statement • If we want to add a blank line after our

The PRINT statement • If we want to add a blank line after our print statement:

# PROGRAM Hello. World. Program: print(“Hello, World”) # END.

# PROGRAM Hello. World. Program: print(“Hello, World”) # END.

# PROGRAM Hello. World. Program: print(“Hello, World”) # END. # PROGRAM Hello. World. Program.

# PROGRAM Hello. World. Program: print(“Hello, World”) # END. # PROGRAM Hello. World. Program. New. Line: print(“Hello, Worldn”) # END.

The PRINT statement • To print out two lines of text we do:

The PRINT statement • To print out two lines of text we do:

# PROGRAM Hello. World. Program. Two. Lines: print(“Hello, World”) print(“I’m here”) # END.

# PROGRAM Hello. World. Program. Two. Lines: print(“Hello, World”) print(“I’m here”) # END.

The PRINT statement • To join two strings together:

The PRINT statement • To join two strings together:

# PROGRAM Hello. World. Program. Joined: print(“Hello, World” + “ I’m here”) # END.

# PROGRAM Hello. World. Program. Joined: print(“Hello, World” + “ I’m here”) # END.

The PRINT statement • To print out the same message 10 times:

The PRINT statement • To print out the same message 10 times:

# PROGRAM Hello. World. Program 10 Times: print(“Hello, World” * 10) # END.

# PROGRAM Hello. World. Program 10 Times: print(“Hello, World” * 10) # END.

The PRINT statement • To print out the same message 10 times, each one

The PRINT statement • To print out the same message 10 times, each one on a new line:

# PROGRAM Hello. World. Program. New. Line 10 Times: print(“Hello, Worldn” * 10) #

# PROGRAM Hello. World. Program. New. Line 10 Times: print(“Hello, Worldn” * 10) # END.

Code Description \ Print a backslash ’ Print a single quote ” Print a

Code Description \ Print a backslash ’ Print a single quote ” Print a double quote a Play a beep n Print a new line t Print a tab

Python: Maths Damian Gordon

Python: Maths Damian Gordon

Some Simple Maths • Let’s look at some simple maths first:

Some Simple Maths • Let’s look at some simple maths first:

# PROGRAM Adding. Numbers: print(10 + 7) # END.

# PROGRAM Adding. Numbers: print(10 + 7) # END.

Some Simple Maths • Let’s make that a bit more fancy

Some Simple Maths • Let’s make that a bit more fancy

# PROGRAM Adding. Numbers: print(“ 10 + 7 = “, 10 + 7) #

# PROGRAM Adding. Numbers: print(“ 10 + 7 = “, 10 + 7) # END.

Some Simple Maths • Let’s try subtraction:

Some Simple Maths • Let’s try subtraction:

# PROGRAM Subtracting. Numbers: print(“ 10 - 7 = “, 10 - 7) #

# PROGRAM Subtracting. Numbers: print(“ 10 - 7 = “, 10 - 7) # END.

Some Simple Maths • Let’s try multiplication:

Some Simple Maths • Let’s try multiplication:

# PROGRAM Multiplying. Numbers: print(“ 10 * 7 = “, 10 * 7) #

# PROGRAM Multiplying. Numbers: print(“ 10 * 7 = “, 10 * 7) # END.

Some Simple Maths • Division is a lot cooler, we can do three kinds

Some Simple Maths • Division is a lot cooler, we can do three kinds of division, – Regular Division – Integer Division – Division Remainder

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) #

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) # END.

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) #

# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) # END. This should give us: 1. 428571

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) #

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END.

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) #

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END. This should give us: 1

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) #

# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END. This should give us: 1 which is how many times 7 divides evenly into 10

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) #

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END.

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) #

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END. This should give us: 3

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) #

# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END. This should give us: 3 which is what is left over when we divide 7 into 10

Some Simple Maths • Can you work this one out?

Some Simple Maths • Can you work this one out?

# PROGRAM Division. Problem: print(((10 / 7 – 10 // 7) * 7) +

# PROGRAM Division. Problem: print(((10 / 7 – 10 // 7) * 7) + 7) # END.

Python: Variables Damian Gordon

Python: Variables Damian Gordon

Using Variables • Variables are easy to use in Python, there is no need

Using Variables • Variables are easy to use in Python, there is no need to declare the type of the variable. • Python will work it out for you (mostly!).

# PROGRAM Variable. Assignment: x = 6 # END.

# PROGRAM Variable. Assignment: x = 6 # END.

Using Variables • And if we want to check the value of the variable:

Using Variables • And if we want to check the value of the variable:

# PROGRAM Variable. Print: x = 6 print(x) # END.

# PROGRAM Variable. Print: x = 6 print(x) # END.

Using Variables • Let’s add 1 to x:

Using Variables • Let’s add 1 to x:

# PROGRAM Add. One. Variable. Print: x = 6 print(x + 1) # END.

# PROGRAM Add. One. Variable. Print: x = 6 print(x + 1) # END.

Using Variables • Let’s try two variables:

Using Variables • Let’s try two variables:

# PROGRAM Two. Variable. Print: x = 6 y = 5 print(x + y)

# PROGRAM Two. Variable. Print: x = 6 y = 5 print(x + y) # END.

Using Variables • If we want to move from integers to real numbers

Using Variables • If we want to move from integers to real numbers

# PROGRAM Real. Variable. Print: x = 6. 56 print(x) # END.

# PROGRAM Real. Variable. Print: x = 6. 56 print(x) # END.

# PROGRAM Another. Real. Variable. Print: x = 6. 0 print(x) # END.

# PROGRAM Another. Real. Variable. Print: x = 6. 0 print(x) # END.

Using Variables • If we want to create character variables

Using Variables • If we want to create character variables

# PROGRAM Character. Variable. Print: x = ‘@’ print(x) # END.

# PROGRAM Character. Variable. Print: x = ‘@’ print(x) # END.

# PROGRAM Another. Character. Variable. Print: x = ‘ 5’ print(x) # END.

# PROGRAM Another. Character. Variable. Print: x = ‘ 5’ print(x) # END.

Using Variables • Now we can see that we can’t do arithmetic with characters:

Using Variables • Now we can see that we can’t do arithmetic with characters:

# PROGRAM Error. Program: x = ‘ 5’ print(x + 1) # END.

# PROGRAM Error. Program: x = ‘ 5’ print(x + 1) # END.

Using Variables • If we want to create String variables

Using Variables • If we want to create String variables

# PROGRAM String. Variable. Print: x = “This is a string” print(x) # END.

# PROGRAM String. Variable. Print: x = “This is a string” print(x) # END.

Using Variables • To get input from the screen, we can do the following:

Using Variables • To get input from the screen, we can do the following:

# PROGRAM Print. Message: print(“Please input a message: ”) New. Msg = input() print(New.

# PROGRAM Print. Message: print(“Please input a message: ”) New. Msg = input() print(New. Msg) # END.

Using Variables • Let’s do the converting temperature program:

Using Variables • Let’s do the converting temperature program:

# PROGRAM Convert. From. Celsius. To. Fahrenheit: print(“Please input your temperature in C: ”)

# PROGRAM Convert. From. Celsius. To. Fahrenheit: print(“Please input your temperature in C: ”) Input. Val = int(input()); print(“That temperature in F is: ”) print((Input. Val *2) + 30) # END.

Convert Description int(x) Convert variable into an integer, e. g. x = “ 10”

Convert Description int(x) Convert variable into an integer, e. g. x = “ 10” int(x) Convert variable into a real e. g. x = “ 10. 5” float(x) Convert variable into an string, e. g. x = 10 str(x) float(x) str(x) Result 10 10. 5 “ 10”

Using Variables • The following words cannot be used as variable names: and as

Using Variables • The following words cannot be used as variable names: and as assert break del elif else except from global if import not or pass print class continue def exec finally for in is lambda raise return try while with yield

A Special Note On Boolean Variables

A Special Note On Boolean Variables

Boolean Variables • In the original version of Python (1989), there was no Boolean

Boolean Variables • In the original version of Python (1989), there was no Boolean type, in Version 2. 2. 1 (2002) True and False constants were added to the built-ins (they were simply set to integer values of 1 and 0 and weren't a different type.

Boolean Variables • Finally in Version 2. 3 (2003) True and False were added

Boolean Variables • Finally in Version 2. 3 (2003) True and False were added in as constants to the __builtin__ module, making them a core part of Python.

# PROGRAM Boolean. Var: x = True print(x) # END.

# PROGRAM Boolean. Var: x = True print(x) # END.

# PROGRAM Boolean. Var: x = False print(x) # END.

# PROGRAM Boolean. Var: x = False print(x) # END.

etc.

etc.