Python Print Damian Gordon Your first Python program
- Slides: 68
Python: Print Damian Gordon
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”)
# PROGRAM Hello. World. Program: print(“Hello, World”) # END.
# # 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 print statement:
# PROGRAM Hello. World. Program: print(“Hello, World”) # END.
# 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:
# PROGRAM Hello. World. Program. Two. Lines: print(“Hello, World”) print(“I’m here”) # END.
The PRINT statement • To join two strings together:
# PROGRAM Hello. World. Program. Joined: print(“Hello, World” + “ I’m here”) # END.
The PRINT statement • To print out the same message 10 times:
# PROGRAM Hello. World. Program 10 Times: print(“Hello, World” * 10) # END.
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) # END.
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
Some Simple Maths • Let’s look at some simple maths first:
# PROGRAM Adding. Numbers: print(10 + 7) # END.
Some Simple Maths • Let’s make that a bit more fancy
# PROGRAM Adding. Numbers: print(“ 10 + 7 = “, 10 + 7) # END.
Some Simple Maths • Let’s try subtraction:
# PROGRAM Subtracting. Numbers: print(“ 10 - 7 = “, 10 - 7) # END.
Some Simple Maths • Let’s try multiplication:
# PROGRAM Multiplying. Numbers: print(“ 10 * 7 = “, 10 * 7) # END.
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) # END.
# PROGRAM Regular. Division: print(“ 10 / 7 = “, 10 / 7) # END. This should give us: 1. 428571
# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END.
# PROGRAM Integer. Division: print(“ 10 // 7 = “, 10 // 7) # END. This should give us: 1
# 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) # END.
# PROGRAM Division. Remainder: print(“ 10 % 7 = “, 10 % 7) # END. This should give us: 3
# 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?
# PROGRAM Division. Problem: print(((10 / 7 – 10 // 7) * 7) + 7) # END.
Python: Variables Damian Gordon
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.
Using Variables • And if we want to check the value of the variable:
# PROGRAM Variable. Print: x = 6 print(x) # END.
Using Variables • Let’s add 1 to x:
# PROGRAM Add. One. Variable. Print: x = 6 print(x + 1) # END.
Using Variables • Let’s try two variables:
# PROGRAM Two. Variable. Print: x = 6 y = 5 print(x + y) # END.
Using Variables • If we want to move from integers to real numbers
# PROGRAM Real. Variable. Print: x = 6. 56 print(x) # END.
# PROGRAM Another. Real. Variable. Print: x = 6. 0 print(x) # END.
Using Variables • If we want to create character variables
# PROGRAM Character. Variable. Print: x = ‘@’ 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:
# PROGRAM Error. Program: x = ‘ 5’ print(x + 1) # END.
Using Variables • If we want to create String variables
# 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:
# PROGRAM Print. Message: print(“Please input a message: ”) New. Msg = input() print(New. Msg) # END.
Using Variables • Let’s do the converting temperature program:
# 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” 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 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
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 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 = False print(x) # END.
etc.
- Damian gordon
- Intitle:"index of" "/usernames"
- Damian gordon
- Iteration.n5 - how many panels?
- How to print zen of python
- First impressions finger print
- What does the following program print?
- Damian sturzaker
- Padre damian de veuster 2215 vitacura
- Padre damian de veuster 2215 vitacura
- Padre damian de veuster 2215
- Damian clancy
- Damian krysztofik
- Damian folch
- Damian urbańczyk partnerka
- Christophe damian
- Damian mac
- Fray damian massanet picture
- Padre damian de veuster 2215 vitacura
- Padre damian de veuster 2215
- Damian harding
- Damian urbańczyk wikipedia
- Toczenie bez poślizgu
- Oscar sandoval md
- A 1017
- Melissa damian
- Damian marli
- Damian czudek
- Money money money team
- Damian viccars
- Tautology in python
- Damian sommerville
- Give us your hungry your tired your poor
- Python subscript operator
- Fundamentals of python: first programs
- Contoh stack dan queue
- Contoh array 1 dimensi
- Python menu driven program
- Program gerobak fried chicken python
- Python program
- Create python program
- Maturity continuum model by stephen
- Breadth first vs depth first
- Sdl first vs code first
- Put first things first activities
- Put first things first meaning
- Entity framework 7 release date
- First to file vs first to invent
- First in first out
- C++ stack vs heap
- First in first out
- First come first serve
- Put first things first definition
- First come first serve calculator
- See do get model
- Habit 3 activities
- Put first things first video
- Put first things first activities
- First aid merit badge first aid kit
- Objectives of first aid
- Put first things first
- Down payment assistance riverside
- The first step in planning a personal activity program is
- First action interview
- Verpleegkundige methodieken
- Gordon allport traits
- Gordon richards drexel
- Gordon willard