Python 01
Hello World with Python
Intro
Print “Hello world”
Similar to Javascript, python has a code to show the result in the console; print()
print("Hello world!");
In Python, variables can be stored in text like name, age, etc… Also, you can use the print function to print variables. ex) print(your_variable)
name = "your_name"
age = 20
Each programming language has a unique style of writing. In JavaScript, yourName = "Dave"
we use this style and call the style camel case. In Python, we use an underscore to divide the text like your_name = "Dave"
we call this style as snake case
We can also do the following to store multiple values in multiple variables. That will be the same result as above.
name, age = "your_name", 20
Why do we want to store data in variables? Because we don’t want to put data every time when we run the program.
print("Hello I'm John I'm 20 years old")
print("Hello I'm Jane I'm 20 years old")
# do this instead
print("Hello I'm " + name + " I'm " + age + " years old")
Variable names can be anything but cannot have the numbers before the variable names.
var = 1 , name2 = 'something' âś…
12var = 1, 2name = 'Elise' ❌
To ask a user input from the terminal, we can use the input()
function.
input("What is your name: ")
Let’s make a simple input that takes the user name and prints with a greeting.
user_name = input('Please enter your name: ')
print('Hello ' + user_name)
To run the .py file in your terminal, type
python your_file.py
from your terminal.
DATA TYPES (string, number)
Strings
Python has many data types, such as strings, integers, booleans, etc. Let’s talk about string type. The string is simply text data.
As we can see from the above (name = "your_name"
), the string should be inside a quote.
Try this from your terminal.
name = your_name
name = "your_name"
name = 'your_name'
The string is a text data type that resides in quote(double or single quotes).
Numbers(int, float)
Also, python has some number
data types.
int
is short for an integer, which is a whole number.
float
is a floating number, which is a number with a decimal point.
Try
num1 = '2'
num2 = '3'
print(num1 + num2)
What happened? It concatenates two strings together.
You can assign numbers in variables without the quotes num1 = 2 , num2 = 3
or use a function int()
num1 = '2'
num2 = '3'
print(int(num1) + int(num2))
For now, just understand that the int()
function takes a string number and returns an integer number value.
Boolean
This data type represents a True or False state. Let’s say we’re building a game. If we want to represent the status of a character that is alive, then we do
character_alive = True # alive
character_alive = False # dead
The important part of boolean is they start with a capital letter True or False
, not true or false
.
Baby Tip Calculator
Tip Calculator App
We use the float()
function instead of int()
for the exact dollar and cents calculation.
food_amount = float(input("Enter Food Amount $: "))
tip_percentage = float(input("Enter your tip percentage %: ")) / 100
# how much am I paying in total?
tip_amount = food_amount * tip_percentage
total = food_amount + tip_amount
print('$ ' + str(total))
Let’s go a little fancy to show the result in the terminal.
F-strings
If we want to use Python expression inside the string constants(inside of the quotes), do this.
name = James
print(f"Hi my name is ${name}")
Using ${}
inside the quotes helps you use Python code inside the string.
And the result turned to follow. Run the code in your terminal and see the difference.
...
# print('$ ' + str(total))
print('\n\n')
print('=================================')
print(f'🍗 Food Amount: ${food_amount}')
print(f'đź’¸ Tip Amount: ${tip_amount}')
print('\n')
print(f'đź’° Total Amount: ${total}')
print('=================================')
Comments