Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

PYCS 02 - Arithmetic and Variables - Colaboratory

This document provides an introduction to basic arithmetic and variables in Python. It demonstrates how to perform addition, subtraction, multiplication, and division in Python. It also introduces variables as a way to store and work with data in Python code. Comments are described as a way to add explanatory notes to Python code for other programmers. Variable naming conventions are also covered, noting that names can include letters, numbers, and underscores but not spaces or special characters.

Uploaded by

godspower bruno
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

PYCS 02 - Arithmetic and Variables - Colaboratory

This document provides an introduction to basic arithmetic and variables in Python. It demonstrates how to perform addition, subtraction, multiplication, and division in Python. It also introduces variables as a way to store and work with data in Python code. Comments are described as a way to add explanatory notes to Python code for other programmers. Variable naming conventions are also covered, noting that names can include letters, numbers, and underscores but not spaces or special characters.

Uploaded by

godspower bruno
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

A Python Introduction for New


(and not-so-new) Programmers
Part 02: Arithmetic and Variables

This Python Notebook is part of a sequence authored


by Timothy R James - feel free to modify, copy, share,
or use for your own purposes.

In this Notebook file, we'll talk about basic arithmetic


in Python, and introduce the concept of variables.

Basic Python Arithmetic

Let's start out with some really simple Python to


perform arithmetic. Adding numbers is a good place
to start. For example, we can enter 1 + 1 (with no
semicolons or other syntax). Push that play button
below, and you'll see the result of Python interpreting
"1 + 1".

1 + 1

We aren't limited to just two numbers at a time. We


can add several numbers together in a single
statement - we do not have to enter them one by one.
Try running each of the code blocks below.

1 + 1 + 1

1 + 2 + 3 + 4

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 1/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

1 + 10 + 100 + 1000 + 10000

Note that you can have 0, 1, or more spaces between


your numbers and the plus sign - both of the following
will work the same way. Spaces (along with tabs) are
whitespace - we can place as many between our
numbers and plus signs as we like.

1+10+100+1000+10000

11111

1 +  10 +100 +    1000 +           10000

11111

Note that it's typical to see a single space between


numbers and symbols. The code above would most
commonly be written like this:

1 + 10 + 100 + 1000 + 10000

We can do more than just simple addition. We can


also perform subtraction. Try running the code below
(I'll stop suggesting that now, but you can keep running
code as much as you want, and I encourage you to do
so).

100 - 23

77

Again, just like with addition, we can subtract several


numbers at the same time.

900 - 300 - 50

We can mix addition and subtraction. Python will


follow the order of operations; addition and

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 2/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

subtraction proceed left to right.

100 - 23 + 7 - 4

If you want to change the order of operations, you can


enclose parts of the calculation in parentheses.

100 - (23 + 7) - 4

We can multiply, too, using an asterisk.

33 * 12

396

Different arithmetic operations can be mixed. Note


that multiplication occurs before addition and
subtraction, so ordering matters.

2 * 10 - 4 + 3 * 6

If you want to change the natural order, again, you can


use parentheses.

2 * (10 - 4) + 3 * 6

Division uses a forward slash.

20 / 5

Note that when necessary, the result of a division


operation can be a decimal. For example, 12 / 5 will
result in 2.4 .

12 / 5

2.4

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 3/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

If we want to use integer division (also called floor


division), we use a double forward slash. This drops
off the decimal portion - so 12 // 5 will give you 2
and not 2.4 .

12 // 5

There are times where you might need an integer, or


you need to work with only the integer part of a
number - especially in more complex algorithms.

Try running each of the code blocks below, so that


you can see the difference between regular division
and floor division.

50 / 11

4.545454545454546

50 // 11

We're not just limited to integer numbers - we can


also use decimal numbers.

3.5 * 17.2

60.199999999999996

2.5 * 10.25 - 4.75 + 3.5 * 6.25

Comments

Comments are a way to explain what's going on in


your code, or why it's happening. It's a good way to
write notes to your future self when you have to read
it later; comments might also be useful to other

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 4/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

people who might have to read your code. Python


ignores your comments - they're just for human
readers.

Comments are simple in Python, and it's a good idea


to add some substantial comments to your code for
clarity. In Python, there are 2 types of comments;
single line comments and multi-line comments.

One thing to note: if you run the code blocks below,


you'll find that they don't do anything - and that's the
point! Again, Python ignores the comments - they're
just to help other people understand what the code is
doing, or why it's doing it.

Single line comments are created using the


octothorpe (aka the pound or number sign).

# Most comments in Python start w/ an octothorpe (also known as the pound sign).

# You can have as many of these as you like as long as they start with '#'.

Multi line comments can use 3 single quotes.

''' Multi-line comments in Python 

    can be created with 3 single quotes '''

' Multi-line comments in Python \n can be


created with 3 single quotes '

Multi-line comments can also use 3 double quotes.

""" 

  Three

  double

  quotes

  is

  also

  ok

"""

You're encouraged to add comments to clarify your


own code; it's good to write comments any place
where what your code is doing is unclear.
https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 5/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

Variables

Variables are a place for us to store data in our


Python code. Computers have memory, too, and by
using this memory we can solve more and more
complex problems.

To declare a variable in Python, we just give it a


descriptive name and assign a value to it. For
example, the code below will create a variable called
number and assign the int value 4 to it.

number = 4

If we want to see what the value of number is in a


Colab Notebook, we just enter the variable name. If
you ran the code block above, and then run the code
block below, you should see 4 .

number

Variable Names

In Python, our variable names will typically start with


a letter. Variable names can include letters, numbers,
and underscores. Variable names can't include
spaces.

Some valid variable names:

number
number5
number_5
variablename
a_long_name_is_ok

Some variable names that aren't valid:


https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 6/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

5numbers - invalid, because variable names


can't start with a number.
spaces are not ok - invalid, because variable
names can't have spaces.
variable-name - invalid, because variable
Eyoas Bekele
names can't contain hyphens. 2:49 PM Yesterday

sales_tax = purchase * tax


More on Python variable naming rules is available
here.
Tobechukwu Obi
5:11 AM Today

Another important point about variable names is that sales_tax = tax * purchase
they are case-sensitive. This means that lower-case
letters and upper-case letters are completely different Eyoas Bekele
2:51 PM Yesterday
in Python. For example, the following code block uses
2 different variable names. area_of_triangle = 1/2 * (base * height)

value = 50
Tobechukwu Obi
Value = 100
5:11 AM Today

area_triangle = 0.5 * base * height


value starts with a lower case v. Value starts with
an upper case V.
Eyoas Bekele
2:38 PM Yesterday
(edited 8:58 PM Yesterday)
value
Do we write the answers underneath ? or is
there a different way of submitting our work
?
Value

Timothy James
8:58 PM Yesterday

Using Variables these are not collected or graded; they're for


your own purposes.

Variables can be used to perform calculations. We


can start writing sequences of lines of code that Eyoas Bekele
2:52 PM Yesterday
perform particular operations.
circumference = diameter * pi

number_five = 5

one_more_than_five = number_five + 1
Tobechukwu Obi
one_more_than_five
5:13 AM Today

circum_circle = diameter * pi

# circum_circle = circumference of a circle

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 7/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

Try It!
Variables and Calculations

Now that you know some Python basics, how would


you teach someone basic arithmetic in Python? Use
the code block below to write and test a few
examples.

If sales tax is 6%, how much sales tax do you


pay on a purchase of $7.98?
What's the area of a triangle if its height is 8.2
and its base is 3.5?
Assuming pi = 3.14, what's the circumference of
a circle if its diameter is 19?

tax = 0.06

purchase = 7.98

height = 8.2

base = 3.5

# the area of a triangle is one half its base times its height.

pi = 3.14

diameter = 19

# circumference is the diameter multiplied by pi.

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 8/9
1/10/23, 1:14 PM PYCS 02 - Arithmetic and Variables - Colaboratory

Colab paid products


-
Cancel contracts here

check 0s completed at 11:06 PM

https://colab.research.google.com/drive/1Ek8Hlp9QZcNsXkVZD16ZJEMkICBnFCgI?usp=sharing#scrollTo=ukIrOqyi9ZwR&printMode=true 9/9

You might also like