How To Learn Python 3 For Expressions and Variables

Today we will study how to use Python to create expressions and variables that will be used later in our programs, specifically we will learn the type of variables that Python has and it's common uses, furthermore we will study how use this expressions in order to print simple result and create conditions that we will use through our programming, now it's time to begin our programming lesson with Prograp, just for you!

Data Type in Python

As we can see in the picture (1), a computer receives an input from the user, Python allow us to use our common language to communicate with the machine. That's why we can say that Python is a high level language. This means that Python allow to the user to abstract orders to the computer and use instructions of the programming language that are similar to our language.

The reason behind this need is the fact the fact that the computer understand a language called "Machine Language". The machine language can be seen a language composed by 0 and 1 and all the combinations of any length possible.

Input and Output process in the computer through a programming language.

Taken from https://www.coursera.org/learn/aprendiendo-programar-python/home/welcome

The principal problem of this language is the fact that our computer can need a big amount of 0 and 1 joined to express an abstract concept, as shown in the image (2) we can see how much information the computer uses to deal with the needs of the user.

The interpretation of the machine language as combinations of 0 and 1.

Taken from https://www.coursera.org/learn/aprendiendo-programar-python/home/welcome

The programming language give us representations for a set of values, such representations are called Data Type. The primitive Data type that Python provides are the numeric type float and int, the text type str and the logic type or Boolean type bool. We will consider each type separately.

Data Type int

The data type int is used to represent integer numbers, they are used to answer questions such as:

  • What year were you born in?
  • How much coins do you have?
  • How much friends do you have?

Examples of int type data are 5, 77,  0, -254, 2017, 1, 330, 4567898765.

You can identify them in Python using the following command, type(2017), if you do this then your IDE will give you the answer <class 'int'>.

Data Type float

The data type float is used to represent  numbers with decimal representations, they are used to answer questions such as:

  • What is my final grade for this course?
  • What is the temperature of this room?
  • What is my actual weight in kilograms?

Examples of float type data are 0.77, 35.05, -109.678976, 10,3333333, 12.0.

You can identify them in Python using the following command, type(2017.78787), if you do this then your IDE will give you the answer <class 'float'>

.

Data Type str

The data type str is useful to represent sequences or chain of data (strings). Some examples of string data are the following: "K", "7", "I'm learning to program", "90083076589". In order to obtain string characters you have to put "_____". This type of data can be used to answer the following questions:

  • What is your name?
  • How are you today?
  • What is your cellphone number?

You can identify them in Python using the following command, type("Prograp"), if you do this then your IDE will give you the answer <class 'str'>.

Data Type bool

Bool data can be used to provide logic values in the binary logic, that is True or False. This kind of data can be used to answer the following questions with true or false.

  • Is 6 bigger than 8?
  • Is 8>0 or 7<2?
  • Is this price correct?

If we check the type using our IDE and entering type(True), then we will get <class 'bool'>.

So we learned that:

This is a summary of some basic data types in Python.

Operators and Expressions

After learning how to define expression and variables in Python our next step is to learn how to work with this expression, here we will show the principal ways to operate expression like we operate two numbers using sum. Our main gaol in this section is to make operations with data. We'll divide this procedure following a similar way than the precedent part:

Here we show a graphic rperesentation of the operators and data in Python.

Taken from https://www.coursera.org/learn/aprendiendo-programar-python/home/welcome

Operator for numeric values. Arithmetic Operators

The operators for numeric values relate to some arithmetical operators used to perform calculations using int and float values, we have the following operators:

Operation

Sign

Input

Output

Sum

+

>>>9+8

>>>17

Substraction

-

>>>6-5

>>>1

Multiplication

*

>>>5*7

>>>35

Division

/

>>>44/11

>>>4

Inverse additive

-(__)

>>>-(5)

>>>-5

Exponentiation

**

>>>2**10

>>>1024

Integer part

//

>>>19//8

>>>2

Remainder

%

>>>19%8

>>>3

The evaluation order taken by Python is the same than the order from our Math knowledge, it means that Python takes care of the order of precedence and the associativity, for example if we put the following expression in Python

>>> (3+5//4-2)-2**4+3*(7-2)

Then evaluating in the given order we'll have  (3+5//4-2)-2**4+3*(7-2)=(3+1-2)-16+3*5=1. So the output will be >>>1. So we can check that the operators are applied from left to right, following the precedence and the associativity.

Operator for numeric values. Comparison Operators.

They are applied to int or float data type, they will give as result a bool type. The comparison operators are:

Operation

Sign

Input

Output

Less than

<

>>>7<8.98

>>>True

Less than or equal to

<=

>>>17>=8

>>>False

Bigger than

>

>>>10>6.9876

>>>True

Bigger than or equal to

>=

>>>7>=2

>>>True

Different from

!=

>>>7 != (5+2)

>>>False

Equal to

==

>>>16 == (2**4)

>>>True

Operators for logic type. Logic or Boolean operators.

They're applied to bool data type. This type are the following, not (Logical negation), and (Logical Conjunction) and or (Logical Disjunction). In the following picture we can see the truth table associated with each operator:

This is the true table of some common boolean operator in Python.

Let's analyze each Truth table.

  • Logical Negation or not: takes a Boolean and change it's value to the opposite, for example if we type >>>not 8>9    then we'll get >>>True because we took a False statement and change to the opposite value, that is False. Now if we type  >>>not 7.8<9.12 then we'll get >>>False because we took a true statement and the opposite of True is False. 
  • Logical Conjunction or and: the and operator takes two statements and give a True value only if both statements are True, otherwise is false.
  • Logical Disjunction or or: the or operator takes two statements and give a False value only if  both statements are False, otherwise we'll obtain True always.

Here we have an example of this operators in context:

Operation

Input

Output

not

>>> not 4>9

True

and

>>>14<19 and 7==8

False

or

>>> 9!=8 or 7>1

True

Operators for data type. Adding and multiplication strings.

If we take two strings like >>>"I'm"+" your father." will have as output >>>"I'm your father", it means that we've connected two strings using +, in this way we can add strings. In case you obtain >>>"I'myour father." then remember to put one space after the quotation marks like this >>>"I'm"+" your father." or we can use the following space  >>>"I'm"+" "+"your father." . In the case we wanna repeat a string a given number of times then you can use >>>4*"Ha" this will give as result >>>"HaHaHaHa", as you can see this order helped us to obtain 4 times the string "Ha", as well we could use >>>"Ha"*4 and this will give us as result >>>"HaHaHaHa", it means that the operation * is commutative.

The summary of this section is:

This is a short summary of the operators of data in Python

Assignation of variables name in Python.

In order to make assignations of variables in Python we can use mostly any word (this have some exceptions as we will see in the section Forbidden assignation words in Python). For example, we wanna obtain the value of the water fee in our house, we know that each cubic meter has a value of $0.3/m^3, the garbage fee is a fixed value and it's equal to $2. We wanna calculate the value of the water fee if we spent 10 cubic meters of water, this is easy if we use Python as a calculator, we only open and type >>>10*0.3+2, then we will get >>>5, so we need to pay $5 for our water fee. When we use Python as a calculator we're running a fixed program with fixed values that need to be changed again and again when we perform a calculation, in this case that is an easy task because we only need to change the number of cubic meters and we have the price.

But we can do something more efficient, for example we can create a program which ask the cubic meters value at first and then provides the answer. In order to introduce the cubic meters we can use >>>a=float(input()), this command creates an assignation (=) to the string that will be typed by the user. Even if you write a number our system will classify it as a string, so in order to change the variable type to a numeric value we just type float(____), then if the string inside the parenthesis is a number it will be transformed to a float data. The command input allow to the user to introduce information to the program. Let's say that a is the number of cubic meters is a, then the following can be a simple sequence for a program which calculates the water fee.

Here we show a short program to calculate the fee of a service.

Forbidden assignation words in Python

As we could see, we can assign mostly any name to a variable, but this rule has some exceptions. In the case of Python the following words aren't allowed to be used as names of variables because they're predefined and relates to certain instructions or names given in the process of programming in Python. So, we'll avoid to use them as words for calling variables.

This is a list of banned words in Python.

In our next article we'll learn more about the assignment process of Python and we will study more deeply how to create type conversions and how Python classify different operations between data from different type, then we'll be ready to study Control Flow structures such as if, else, elif and while. Thanks for reading, this is Prograp, just for you.

Kamiko

Hi, everyone! I am just a girl who is a beginner to study web and programming. Sometimes, knowledge of Internet makes us confused, so I will explain such the topics to make you understand very well through the eyes of a beginner. Hopefully, my contents will help you to be sucessful and give you joy and fun!

No comments:

Post a Comment