Python: Python basic
print()
print() function uses to print to the output (screen) information or value of variable
Example:
print("Hello Python")
Practical:
Using print() function to print some information about you: name, age, school/university, hobies, unlike.
Input a string:
To input a string you can use input() function.
Example:
input("Please input your name: ")
If you want to save input information you can do as following:
name = input("Please input your name: ")
where:
name: name of variable. The name is optional, that you can set your own.
= : asignment or store. You use this operator to store the input information to the variable.
Practical:
Input some information about you: name, age, school, hobies, unlike.
Mathematics operators:
We have mathematics operators as following: + (addition), - (subtraction), * (multiple), / (float division), // (integer division), % (module division), ** (exponetial)
Example:
a = 9
b = 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** 2)
Practial:
Separating a number to number letters. For example, number 153 can separate to 1, 5, 3.
Coparison operators:
We have comparison operators as following: > (greater than), >= (greater than or equal), < (less than), <= (less than or equal), == (equal), != (different)
Example:
a = 9
b = 5
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
Logical operators:
We have logical operators as following: and, or, not.
Where:
and: Requires two both conditions at left and right of it must be True than it returns True, otherwise it returns False.
or: Requires either left condition or right condition must be True than it returns True, otherwise it returns False.
Example:
a = 9
b = 5
print(0 <= a and a <= 10) # It will print: True
a = 19
print(0 <= a and a <= 10) # It will print: False
print(0 <= a or a <= 10) # It will print: True
print(not(0 <= a or a <= 10)) # It will print: False
if-else condition:
To understanding about if-else condition, we see a following example:
avgMark = float(input("Input an average mark: "))
What is academic ability?
if avgMark >= 9 and avgMark <= 10:
print("Excellent")
elif avgMark >= 8 and avgMark < 9:
print("Good")
elif avgMark >= 6.5 and avgMark < 8:
print("Pretty")
elif avgMark >= 5 and avgMark < 6.5:
print("Average")
elif avgMark >= 0 and avgMark < 5:
print("Retake the exam")
else:
print("This average mark is invalid!")
for loop:
for loop can be understood through some following examples:
example 1: Print numbers from 0 to 9:
for i in range(10):
print(i)
example 2: Print numbers from 1 to 10:
for i in range(1, 11):
print(i)
example 3: Print numbers from 1 to 10 with step = 3:
for i in range(1, 11, 3):
print(i)
example 4: Print numbers from 10 down to 1:
for i in range(10, 0, -1):
print(i)
example 5: Print numbers from 10 down to 1 with step = 3:
for i in range(10, 0, -3):
print(i)
while loop:
The while loop use to loop statements that infinite.
Example: Input a float number that greater than or equal 0 and less than 100.
number = float(input("Input a number: "))
while not(0 <= number and number < 100):
number = float(input("Re-input a number: "))
Function:
A function uses to execute a specific task.
The function makes a program to become clear, easy to read, maintain, update.
We can use a function many times, many where in a program.
To define a function, we use a syntax as follows: