04 - Objects, Type Casting

Objects

have states and behaviors.

State- data, information about the object itself

Behavior- functions, procedures, things it can do

Everything in Python is an object

ex.

x = 5

  • An int object.

  • Has some information about itself- the value 5

  • Some behaviors that this object of type int can do are add, subtract, multiply, etc.

Objects are defined in classes

Classes have fields for data and function definitions.

Type Casting

The input function is used to get some information from a user of our program.

It always returns a string.

Even if the user were to type the number 5, it would return it to you as a string, like β€œ5”

If we want to do anything with that number (add, subtract), we need to change it into an int!

Type Casting- returns to you a new object of the type you call.

typecasting.jpg
  • Calling the str function on an int argument will give you a new object (a string) containing only the number

  • Calling the int function on a string argument (containing only numeric characters) will give you a new object (an int) with the value of the number in the string.

  • Calling the float function on an int argument

str(), int(), float() are all functions that return a new object of the type you called.

The original object will remain unchanged. (can reassign variable, ex. x = 5 , x = float(5) )

ex. Ask the user their name, calculate how many years they have until they turn 100, print a message letting them know how many years they have until they turn 100.

First, we’ll have to ask our user their age, using the input function.

Then, we will need to cast their age to an int so we can calculate the number of years until they turn 100.

There will be three cases to consider,

  • if the user’s age is less than 100 β€”> print β€œyou have __ years until you turn 100” and the __ will be the difference between their age and 100 (100 - age)

  • if the user’s age is exactly 100 β€”> print β€œyou have 0 years until you turn 100” zero is the difference between their age and 100 (100 - age == 0) so actually the same as the first case

  • if the user’s age is greater than 100 β€”> print β€œyou already passed 100”

So, actually I can modify this so that we only have two cases:

  • if the user’s age is less than or equal to 100 β€”> print β€œyou have __ years until you turn 100” the __ will still be the difference between their age and 100

    • Calculate difference between age and 100

    • Cast to string and concatenate and print our message

  • if the user’s age is greater than or equal to 100 β€”> print β€œyou already passed 100”

    • just print message

fn.png

note- I casted difference to a string inside the print statement. Of course, this could have been done on its own line, but this way our code is a bit more concise.

lessthan.png

example calling our function, it prompts the user to enter their age. I typed 22

exactly.png

example calling our function, it prompts the user to enter their age. I typed 100

greaterthan.png

example calling our function, it prompts the user to enter their age. I typed 102

Previous
Previous

03 - Getting User Input

Next
Next

07 - String Manipulation