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

Python Commands

The print command displays text and variables to the user. Variables are created using assignment (=) and can be of type string, integer, or float. The input command prompts the user for input and assigns it to a variable. IF/ELSE statements contain logical expressions to compare values and execute code conditionally. Conditions can include equality, inequality, greater/less than, and logical operators. Nested IF statements and ELSEIF allow for multiple conditions to be checked.

Uploaded by

Suhaylo Javodova
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views3 pages

Python Commands

The print command displays text and variables to the user. Variables are created using assignment (=) and can be of type string, integer, or float. The input command prompts the user for input and assigns it to a variable. IF/ELSE statements contain logical expressions to compare values and execute code conditionally. Conditions can include equality, inequality, greater/less than, and logical operators. Nested IF statements and ELSEIF allow for multiple conditions to be checked.

Uploaded by

Suhaylo Javodova
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Print

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.

a=int(input(“Please enter a whole number: ”))


name=str(input(“Please enter your name: ”))
dh3=float(input(“Enter a number: ”))
IF-ELIF-ELSE

The [if] statement contains a logical expression in which data is compared and a decision is made
based on the result of the comparison.

The syntax is:

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 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.

and Both conditions must be true.

or One of the conditions must be 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.")

A program which displays which number is greater.


a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
if a>b:
print(a,"is greater than",b)
elif b>a:
print(b,"is greater than",a)
else:
print(a,"is equal to",b)

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.")

You might also like