Python Guide
Indonesian version later (or use google translate)
Stuff will be added according to the ICT class
Output
print()
In Functions Guide
Input
input()
In Functions Guide
int(input())
Reads input as an integer
Example:
x = int(input(“Value of x: “))
name = input(“Your Name: “)
IMPORTANT:
Inputting a number in input() will return similarly to “5” (string)
Inputting a number in int(input()) will return similarly to 5 (integer)
Variables
Creating Variables
[variable name] = [value]
Examples:
- x = 6
- y = x / 5
- name = “John”
- online = False
Data Types
Data types get assigned automatically while creating variables
Types:
- String (“one”)
- Int (1)
- Float (1.5)
- Complex (1j, j is imaginary)
- Bool (True/False)
Boolean
Booleans are true/false
You can also get boolean values from comparison operators
Operators
Math Operators
Addition +
Subtraction -
Multiplication *
Division /
Modulus % (Returns division remainder. Example: 5 % 2 = 1)
Exponent **
Assignment Operators
x=5
x += 5 (x = x + 5)
x -= 5 (x = x - 5)
x *= 5 (x = x * 5)
x /= 5 (x = x / 5)
x %= 5 (x = x % 5)
x **= 5 (x = x ** 5)
Comparison Operators
Equal ==
Not equal !=
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Comments
To type a comment:
# [comment]
If Statement
if x > y:
print(“x is bigger than y”)
If statements check if the condition is true (x > y), if it is then it runs the code below it. It is
important to put space before the code in the if statement because Python relies on it.
if x > y:
print(“x is bigger than y”)
else:
print(“y is bigger than x”)
The code below else is used when the comparison of all if’s and elif’s are false
if x > y:
print(“x is bigger than y”)
elif y > x:
print(“y is bigger than x”)
else:
print(“x is same as y”)
Elif is basically else if and is run if the comparison above elif is false