# Introduction to Computation Using Python ## Lesson 2
Expedia Code Academy
### Data types so far We have seen three types of data already: - Numbers: - `0` - `-124.5` - Strings: - `'hello'` - `'I am 99 years old!'` - Booleans: - `True` - `False`
### Casting `1` is an Integer, `1.0` is a Float and `"1"` is a String. But, from a human perspective, the three of them just represent the number 1. Let's find out if Python considers them equal: ``` python print(1 == "1") print(1 == 1.0) print(1.0 == "1") ```
### Casting In order to cast a value or a variable to a specific Type, you wrap them around the following methods: ``` python intFromFloat = int(1.0) intFromString = int("1") print("1.0 and \"1\" are now both Integers:", intFromFloat == intFromString) floatFromInt = float(1) floatFromString = float("1") print("1 and \"1\" are now both Floats:", floatFromInt == floatFromString) stringFromInt = str(1) stringFromFloat = str(1.0) print("1 and 1.0 are now both Strings. But are they equal?", stringFromInt == stringFromFloat) ```
### You can even cast booleans ``` python intToBool = bool(1) floatToBool = bool(1.0) print(intToBool, "is", floatToBool) intToBool = bool(0) floatToBool = bool(0.0) print(intToBool, "is", floatToBool) # From Strings, any non-empty String will be considered True, no matter what's written. emptyStrToBool = bool("") spaceStrToBool = bool(" ") num1StrToBool = bool("1") num0StrToBool = bool("0") trueStrToBool = bool("True") falseStrToBool = bool("False") print("Empty String is", emptyStrToBool) print("The rest is", spaceStrToBool, "and", num1StrToBool, "and", num0StrToBool, "and", trueStrToBool, "and", falseStrToBool) ```
### Why are boolean useful? Think of the program powering Hotels.com. A user can only get exclusive deals if they're a Gold Member... But this means we need to change the price depending on whether this condition is
### What if...? The simplest way to do this is the
statement: ``` python if x > 0: print('x is positive') ``` Syntax is important! You'll need these: - 4 spaces before any **line under the if** - A colon after your boolean
### Indentation The indentation (4 spaces) before each statement determine whether it belongs to the
or not. ``` python if x > 0: print('x is positive') print('a message that always gets printed') ``` ``` python if x > 0: print('x is positive') print('a message that gets printed only when x is positive') ```
### If/else ``` python if x > 0: print('x is positive') else: print('x is negative') ``` The code under `else` gets executed when the if boolean is False. They are mutually exclusive!
### If/elif/else Sometimes there are more than two possibilities, so you need more than True/False or if/else. ``` python if x > 0: print('x is positive') elif x == 0: print('x is equal to 0') else: print('x is negative') ``` `elif` is an abbreviation of “else if” Please note that **only one** branch will be executed, and priority starts from the top!
### Accepting user input What if we could provide data while our code is running? `input` pauses the program until the user types something and presses Enter. We can then use what the user typed as a variable in the code! ``` python users_name = input('What is your name? ') print('Hello, ' + users_name + '!') ```
### Exercise Acquire two numbers and print if the second is bigger, smaller or equal to the first *Hint* for exercise: ``` python # You can use print as follow without carying about casting my_number = 0.5 print('This is my number', my_number) ```