Python Commands
Python Commands
The print command is used to display text and/or variables to the user. These may be of mixed types
and can be separated using [,] (this creates a space) or [+] (this adds 2 strings together).
Syntax
print(“[text]”,[variable name])
print([string]+[string])
e.g.
print(“Good morning.”)
print(“Hello”,name+“.”)
Variables
A variable is a data item that may take on more than one value during the runtime of a program.
Variables are created using [=]. Use [str] [int] or [float] to determine what type of variable this will
be.
Syntax
[variable name]=[type]([value])
e.g.
a=int(3)
name=str(“Matthew”)
it5=float(17.345)
Input
We can create a variable, where the value is given by the user, using the [input] command.
Use [str] [int] or [float] to determine what type of variable this will be.
Syntax
[variable name]=[type](input(“[prompt]”))
e.g.
The [if] statement contains a logical expression in which data is compared and a decision is made
based on the result of the comparison.
if [condition]:
[statements]
if a==0:
print("a is 0.")
Condition
Description
Operator
== If the values of two operands are equal, then the condition is true.
<> If values of two operands are not equal, then condition is true.
If the value of left operand is greater than the value of right operand, then
>
condition is true.
If the value of left operand is less than the value of right operand, then condition
<
is true.
If the value of left operand is greater than or equal to the value of right operand,
>=
then condition is true.
If the value of left operand is less than or equal to the value of right operand,
<=
then condition is true.
[elif] is used in combination with a single [if] to create other possibilities for this decision. Only the
first true statement in a string of [if]-[elif] will be executed. [else] may be used when the program
should execute certain commands when all other [if] and/or [elif] are false. For each [if] we can use
as many [elif] as we like, but only one [else] at the end.
The following are example programs combining all of the above.
A student will not be allowed to sit for an exam if his/her attendance is less than 75%. This program
calculates that percentage and displays whether they are eligible or not.
ch=int(input("Enter the number of classes held: "))
ca=int(input("Enter the number of classes attended: "))
p=(ca/ch)*100
print("You have attended",p,"% of classes.")
if p>=75:
print("Congratulations! You are eligible to sit for the exam.")
else:
print("You do not meet the attendance criteria.")
This program shows [if] statements nested within other [if] statements. It also shows the use of
[and] [or] to combine multiple conditions.
price=float(input("Enter the price: "))
quantity=float(input("Enter the quantity: "))
total = price*quantity
if total > 100:
if total > 500:
print("Total is greater than 500.")
else:
if total < 500 and total > 400:
print("Total is between 400 and 500.")
elif total < 500 and total > 300:
print("Total is between 300 and 400.")
else:
print("Total is between 200 and 300.")
elif total == 100:
print("Total is 100.")
else:
print("Total is less than 100.")