KEMBAR78
Python part two names and types | PPT
Python 2
Variables, Assignment and
Types
Personality Tester
yournumber = input("Please enter a number between 1 and 99: ")
print "You entered " , yournumber
print "Performing personality analysis on your number choice …"
if yournumber < 49:
print "You are truly inadequate"
else:
print "You have delusions of grandeur"
print "That's all folks"
Values
• A basic thing – a letter or a number
• Values have TYPES
• Integers 1, 29485
• Strings “Hello world”
• Float – floating point 3.14159
Variables
• A variable has a value
mynumber
Variable names
• Can’t begin with a number
• Can’t contain spaces or other funnies
• Can’t be a keyword
– Type help() and press return
– Then type keywords in the box.
Assignment
Putting a Value ‘into’ a Variable
• myVariable = 23
• myLongVariable = “Hello how’s it going”
Sometimes written with an arrow
• myVariable 23
Assignment is really a sort of COPY
Why?
Assignment quiz
• What’s the value of myVariable
myVariable = 22
mySecondVariable = 13
myVariable = mySecondVariable
myVariable = str(myVariable)
Statements
• A statement is a piece of code that
Python can execute.
• Lots of statements make a script
• Scripts are the same as programs
Operators
• Operators ‘do things’ to operands
• The simplest operators are the math
+ addition 5+2
- subtraction 5-2
* multiplication 5*2
/ division 5/2
** exponentiation 5**2
% modulo 5%2
Now we can do the first part of the Python Challenge!
A useful program
• Write a Fahrenheit to Celsius converter
• The formula for the conversion is: F = 1.8 * C + 32
• To make this program work you will need to do the
following:
Get the user input using input(“your prompt here”)
Assign the result to a variable.
• That will give you a number, but it will be in a string type.
You’ll need to convert it to an integer type using
int(variablename).
• Now you have an integer, multiply it by 1.8 and add 32
• Print the result – your answer, to the screen with
print variablename
Challenge
• Write a script/program that allows you to
input two numbers and calculates their:
– Product
– Sum
– Difference
– Quotient (one number divided by the other
without any remainder)
– Remainder (remainder when one is divided by
the other)
– Exponent (one number to the power of the other)
Challenge 2
• Create an object
• Find its type
– Print (type(myobject))
• Find its id
– Print (id(myobject))
• What is that?

Python part two names and types

  • 1.
  • 2.
    Personality Tester yournumber =input("Please enter a number between 1 and 99: ") print "You entered " , yournumber print "Performing personality analysis on your number choice …" if yournumber < 49: print "You are truly inadequate" else: print "You have delusions of grandeur" print "That's all folks"
  • 3.
    Values • A basicthing – a letter or a number • Values have TYPES • Integers 1, 29485 • Strings “Hello world” • Float – floating point 3.14159
  • 4.
    Variables • A variablehas a value mynumber
  • 5.
    Variable names • Can’tbegin with a number • Can’t contain spaces or other funnies • Can’t be a keyword – Type help() and press return – Then type keywords in the box.
  • 6.
    Assignment Putting a Value‘into’ a Variable • myVariable = 23 • myLongVariable = “Hello how’s it going” Sometimes written with an arrow • myVariable 23 Assignment is really a sort of COPY Why?
  • 7.
    Assignment quiz • What’sthe value of myVariable myVariable = 22 mySecondVariable = 13 myVariable = mySecondVariable myVariable = str(myVariable)
  • 8.
    Statements • A statementis a piece of code that Python can execute. • Lots of statements make a script • Scripts are the same as programs
  • 9.
    Operators • Operators ‘dothings’ to operands • The simplest operators are the math + addition 5+2 - subtraction 5-2 * multiplication 5*2 / division 5/2 ** exponentiation 5**2 % modulo 5%2 Now we can do the first part of the Python Challenge!
  • 10.
    A useful program •Write a Fahrenheit to Celsius converter • The formula for the conversion is: F = 1.8 * C + 32 • To make this program work you will need to do the following: Get the user input using input(“your prompt here”) Assign the result to a variable. • That will give you a number, but it will be in a string type. You’ll need to convert it to an integer type using int(variablename). • Now you have an integer, multiply it by 1.8 and add 32 • Print the result – your answer, to the screen with print variablename
  • 11.
    Challenge • Write ascript/program that allows you to input two numbers and calculates their: – Product – Sum – Difference – Quotient (one number divided by the other without any remainder) – Remainder (remainder when one is divided by the other) – Exponent (one number to the power of the other)
  • 12.
    Challenge 2 • Createan object • Find its type – Print (type(myobject)) • Find its id – Print (id(myobject)) • What is that?