0% found this document useful (0 votes)
9 views29 pages

Python Basics: Variables and Operators

The document provides an introduction to Python programming, covering its basic structure, syntax, and variable types. It explains how to perform arithmetic operations, use comparison and logical operators, and implement conditional statements with if-else blocks. Additionally, it emphasizes the importance of variable types and includes practical examples and quizzes to reinforce learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

Python Basics: Variables and Operators

The document provides an introduction to Python programming, covering its basic structure, syntax, and variable types. It explains how to perform arithmetic operations, use comparison and logical operators, and implement conditional statements with if-else blocks. Additionally, it emphasizes the importance of variable types and includes practical examples and quizzes to reinforce learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding & Data

Visualization

Dr. Zakaria El Hathat


Department of Supply Chain & Information Systems
Rabat Business School, International University of Rabat
Course Objective
Today’s Topic
Python Basics

Python Basics

Understand the basic structure and syntax of Python programming language.

Write your own simple Python scripts.


Guido van Rossum
Creator of the Python programming language

Python was Invented in the Netherlands, early


90s by Guido van Rossum.
Open sourced from the beginning.
Used by Google from the beginning.
Increasingly popular.
Python as a calculator

Let us calculate the distance between Rabat and Marrakech in meters (m)
Great calculator but how can we make it store
values?
Variables

We can store values by defining variables.


Can later be called by the variable name.
Variable names are case sensitive and unique.
We can now reuse the variable distance_To_Marrakech_Meters in the next block without
having to define it again!
Types of variables

Variables actually have a type, which defines the way it is stored.

Type Declaration Example Usage


Integer int x = 124 Numbers without decimal point
Float float x = 124.56 Numbers with decimal point
String str x = “Hello world” Used for text
Boolean bool x = True or x = False Used for conditional statements
NonType None x = None Whenever you want an empty variable
Why should we care about variable types?
Important lesson to remember!
We can't do arithmetic operations on variables of different types. Therefore, make sure that
you are always aware of your variables’ types!
Casting types

Luckily Python offers us a way of converting variables to different types.


Casting is the operation of converting a variable to a different type

Similar methods exist for other data types: int(), float(), str()
Quick quiz

What will be the result?

‘710’
Arithmetic operations
Symbol Task performed Example Result
Similar to actual Mathematics.
+ Addition 10 + 2 12
Order of precedence is the same as - Subtraction 10 - 2 8
in Mathematics. / Division 9/2 4.5
% Mod 10 % 2 0
* Multiplication 3*2 6
// Floor division 9 // 2 4
** Power of 3 ** 2 9
Comparison operators
Return Boolean values (i.e. True or Operator Output
False). x == y True if x and y have the same value
Used extensively for conditional x != y True if x and y don’t have the same value
statements x<y True if x is less than y
x>y True if x is more than y
x <= y True if x is less than or equal to y
x >= y True if x is more than or equal to y
Comparison examples

Note that = is not the same as ==

What will be the result?

False
Logical operators
Allows us to extend the conditional Operator Result
logic. x or y True if at least one is True

Will become essential later on. x and y True only if both are True
not x True only if x is False
Strings
Can be added.
Can be multiplied.
Can be multiple lines.
These are called methods and add extra functionality to the String.
Printing
When writing scripts, your outcomes aren't printed on the terminal.
Thus, you must print them yourself with the print()
Quick quiz

Do you see anything wrong with this block?


Quick quiz (solution)

A more generic way to fix it


Commenting
Useful when your code needs further explanation. Either for your future self and anybody
else.
Useful when you want to remove the code from execution but not permanently
Comments in Python are done with #
If Else
Fundamental building block of software.
If Else example
Extending if-else blocks
We can add infinitely more if statements using elif.
elif = else + if which means that the previous statements must be false for the current one
to evaluate to true.
Extending if-else blocks example
We can add infinitely more if statements using elif.
elif = else + if which means that the previous statements must be false for the current one
to evaluate to true.
Quick quiz

What would happen if both conditions are True?


Quick quiz (solution)

It will only show “Eligible for Merit Scholarship.”


Python first checks the if statement: gpa >= 3.9. The value 3.9 >= 3.9 is True. The program
executes the code in the if block, printing "Eligible for Merit Scholarship.“
After executing the if block, the program skips the rest of the if-elif-else chain entirely. It does
not even look at the elif or else conditions.

You might also like