Allison is coding...

Notes| Learn Python in 1 hour

A note for Learn Python in 1 hour! 🐍 (2024).

Variable: string, integer, float, boolean.

Arithmetic = +, -, *, /(division that returns a float), //(integer division), %(remainder)

Typecasting, the process of converting a variable from one data type to another.

str()
int()
float()
bool()

Input function:

input("Enter your name: ")

If statements = execute some code only if a condition is True

Logical operators = evaluate multiple conditions (or, and, not)

While loops = used to repeat of code as long as a condition remains “True”, recheck the condition at the end of the loop

For loop = used to iterate over a sequence (string, list, tuple, set), repeat a block of code an exact amount of times

word = "information"
for letter in word:
     print(letter)

List, Tuple and Set

List [ ] = mutable, most flexible

fruits = ["apple", "orange", "banana", "coconut"]
  • fruits[0] = "mango", sign a new element to index 0
  • fruits.append("lychee"), add an element to the end
  • fruits.remove("banana"), remove certain element from list
  • fruits.pop(0), remove certain element at an given index
  • fruits.clear(), clear the list

Tuple ( ) = immutable, faster
Can’t be changed.

fruits = ("apple", "orange", "banana", "coconut")

Set {} = mutable (add/remove), unordered
No duplicates, best for membership testing.

fruits = {"apple", "orange", "banana", "coconut"}

Can use function like fruits.add("pineapple") , fruits.remove("coconut") and fruits.clear().
It can’t work with index.

If print the set, the position will change each time.