0% found this document useful (0 votes)
10 views3 pages

Python Basics: Functions, Variables, Operators

This document serves as a Python guide covering input/output functions, variable creation, data types, operators, comments, and conditional statements. It explains how to read input, assign variables, and utilize various operators including mathematical and comparison operators. Additionally, it provides examples of if statements and the importance of indentation in Python syntax.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Python Basics: Functions, Variables, Operators

This document serves as a Python guide covering input/output functions, variable creation, data types, operators, comments, and conditional statements. It explains how to read input, assign variables, and utilize various operators including mathematical and comparison operators. Additionally, it provides examples of if statements and the importance of indentation in Python syntax.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like