Programming with Python Class 4

Variables and operations

In this class we will see the data types that can be used in Python.

Integers and reals

An integer variable is the one that stores a number without decimals

To handle decimal numbers we will use the float (real)

Example integers

age = 35# Show age on screen
print (“My age is”, age)

. variable type not declared

. the symbol = is used to assign values

Real example

vreal = 23.5

# Show a real per screen
print (“real value”, vreal)

. we can show the strings with double or single quotes.

Mathematical operations

We can use the usual symbols to do calculations.

sum
subtraction

* multiplication

/ division

// integer part of division

% modulus

Example

5 + 1

6-2

10 * 3

10/2

14 // 5

9% 2

Text strings

A text string is a non-modifiable sequence of characters delimited by quotation marks “or‘

Special characters can be included with the escape character \

\ n line break

\ ”Include quotation marks in the text

\ ’Include quotes in the text

\ t tab

If the string is very long you can put strings on different lines and python concatenates them

e.g

print (“first part of the string”
“second string”)

Boolean

Variables that can take the value True or False

Comparisons

They are used to evaluate the relationship between two values ​​or expressions and give a boolean value

> older

> = greater or equal

<minor

<= less than or equal

== equal

! = different

e.g

10 <12 evaluates to True

20 == 10 evaluates to False

5! = 10 evaluates to True

Logical operators

They are used to evaluate combinations of Boolean expressions

and

or

not

e.g

(10 <15) and (2 == 2) evaluates to True

not (10 <15) evaluates to False

Conditional execution

Throughout a program we will have the need to execute a block of statements depending on some condition.

The simplest form of conditional execution is the if statement

e.g

if x> 0 then:

print (“x is positive ..”)

The if statement must end in: and the associated statement block must be indented.

We can execute alternative conditions with the form if .. else

e.g

if x> = 0 then:

print (“x is positive ..”)

else:

print (“x is negative ..”)

Conditions can be chained (elif is an abbreviation of else if)

e.g

if x> 0 then:

print (“x is positive ..”)

elif x = 0 then:

print (“x is zero ..”)

else:

print (“x is negative ..”)

Example

Price calculation with VAT.

The user will be asked to enter a price and the VAT to apply and the program will show us the price with the calculated VAT.

# – * – coding: utf-8 – * –
# assigning values, requesting data from the user
print (“Enter the price without VAT:”)#read price as float
price_without_iva = float (input ())print (“\ nEnter the VAT to apply:”)#read iva as float
iva = float (input ())price_with_vat = price_without_iva + price_without_iva * iva / 100;# show data on screen
print (“\ nThe price with VAT is”, price_with_iva)

Notes:

. the first line # – * – coding: utf-8 – * – is used to indicate the coding of the code, otherwise it will not be able to use accents

. the input () function is used to request a value from the user and returns the entered text as a string

. To indicate that the value returned by input () is interpreted as real, an explicit conversion of the form float (input ()) must be done.

In C the program would be something like

int main (void)
{
/ * declare variables * /
float price_without_iva;
float price_with_iva;
int iva;/ * assigning values, requesting data from the user * /
printf (“Enter the price without VAT:”);
scanf_s (“% f”, & price_with_iva);
printf (“\ nEnter the VAT to apply:”);
scanf_s (“% d”, & iva);price_with_vat = price_without_iva + price_without_iva * iva / 100;/ * display data on screen * /
printf (“\ nVAT with VAT is% .2f \ n”, price_with_iva);
return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *