Python — Basic Review before diving deeper 01
Want to recall the memory of Python easily?
This is the note taken while I was learning Python “again”, hope it helps you too.
There are additional links in the article for you to understand further information.
Directory
- print out
- variables
- typecasting
- user input
- math methods
- if/elif/else statement
- while loop
- for loop
❤️Good Resources
How to print out?
There are three ways.
age = 18
print("You are", age, "years old.")
print("You are " + str(age) + " years old.")
print(f"You are {age} years old")Variables
- int
- float
- string
- boolean
age = 18 # integer
gpa = 3.5 # float
name = 'Apple' # string
dessert = "cake"
grade = 'D' # string
isValid = True # boolean
print(f"Your age is {age}.")
print(f"GPA is {gpa}.")
print(f"Name is {name}.")
print(f"Favorite dessert is {dessert}.")
print(f"Grade is {grade}.")
print(f"isValid is {isValid}.")Multiple values in one line
a, b, c = 1, 2, 3Typecasting
Manually convert a variable from one data type to another.
- int()
- str()
- float()
- bool()
For example (Explicit)
age = 18 # integer
print(type(age))
age = float(age) # float
print(type(age))Implicit cast
a = 8
b = 4
c = 2.0
ans1 = a / b
ans2 = a / c
print(ans1)
print(type(ans1))
print(ans2)
print(type(ans2))User Input
name = input("What's your name?")
age = input("How old are you?")
print(f"Heyyya {name},")
print(f"You are {age} years old.")Input value was string by default.
name = input("What's your name?")
age = input("How old are you?")
age = age + 1 # error
print(f"Heyyya {name},")
print(f"You are {age} years old.")Correct way is below.
name = input("What's your name?")
age = float(input("How old are you?"))
age = age + 1 # error
print(f"Heyyya {name},")
print(f"You are {age} years old.")Math
arithmetic operators
The comment in each section is same as the next line.
i.e. apple = apple + 1 is as same as apple += 1
apple = 5
# Addition
# apple = apple + 1
apple += 1
print(apple)
# Substraction
# apple = apple - 1
apple -= 1
print(apple)
# Multiplication
# apple = apple * 2
apple *= 2
print(apple)
# Division
# apple = apple / 2
apple /= 2
print(apple)
# Power
# apple = apple**2
apple **= 2
print(apple)Get Yen Wang’s stories in your inbox
Join Medium for free to get updates from this writer.
Math module
import math
print(math.pi)
print(math.e)Built-in Functions
a = 3.14
b = -3
num1 = round(a)
num2 = abs(b)
num3 = pow(2, 3)
print(num1)
print(num2)
print(num3)Exercises
import math
# area
r = float(input("Enter one number: "))
area = math.pi * r
print(area)Hypotenuses
import math
# hypotenuses
a = float(input("Enter one side:"))
b = float(input("Enter another side:"))
c = math.sqrt(pow(a, 2) + pow(b, 2))
print(f"Side C = {c}")if statement
- if
- elif
- else
if <expression>:
<statement>
elif <expr>:
<statement>
else:
<statement> expression can be conditional( age < 18), boolean, values.
age = float(input("Enter your age: "))
if age >= 18:
print("You are allowed to play the roller coaster.")
else:
print("You cannot enter the facility.")isValid = True
if isValid:
print("You are allowed to play the roller coaster.")
else:
print("You cannot enter the facility.")# True
if 1 == 1:
print("You are allowed to play the roller coaster.")
else:
print("You cannot enter the facility.")not
isValid = True
if not isValid:
print("You are allowed to play the roller coaster.")
else:
print("You cannot enter the facility.")While loop
num = int(input("Enter a # bewtween 1 to 5:"))
while num < 1 or num > 5:
print(f"{num} is not valid.")
num = int(input("Enter a # bewtween 1 to 5:"))
print(f"The # is {num}")not
when you need a solution to quit.
desc = input("Type something:(q to quit):")
while not desc == "q":
print(f"You description is {desc}.")
desc = input("Type something:(q to quit):")If you notice, some questions are repeated.
Python doesn’t have do-while loop,
like what’s said in this article — https://www.freecodecamp.org/news/python-do-while-loop-example/
Do While in C,
#include <stdio.h>
int main(void)
{
int i = 10;
do {
printf("the value of i: %i\n", i);
i++;
}
while( i < 20 );
}This is how you can achieve the same effect manually.
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
breakLet’s get rid of redundant lines.
for loop
You can iterate over a range, string, sequences, etc.
— bro code
for i in range(1, 11):
print(i)
print("Weeeeeeeee🎆")Another usage,
for i in range(1, 11, 3):
print(i)
print("Weeeeeeeee🎆")continue & break
for i in range(1, 40):
if i == 13:
continue
elif i == 17:
break # stop here
else:
print(i)Nested loop
A loop within another loop(s).
rows = int(input("Enter the # of rows: "))
columns = int(input("Enter the # of columns: "))
for i in range(rows):
for j in range(1,columns):
print(j, end=" ")
print() # break the lineIf you didn’t add end = “ ”(There is a space between each number per row.
rows = int(input("Enter the # of rows: "))
columns = int(input("Enter the # of columns: "))
for i in range(rows):
for j in range(1, columns):
print(j)
print()







