Expressions, Data Types, Variables
Computer Programs
A computer program is a collection of instructions that a computer can follow to perform a specific task. A software is a collection of programs wrapped up as a product that can be used e.g. Microsoft Office. A software application is a software that is meant to be used by an end user, as opposed to software used by other software (e.g. the software that drives a hard disk). Programming is the task of writing programs. Software Engineering is the task of creating software.
Points to note:
- A computer can simply follow instructions exactly. It cannot think for itself although there are software that can simulate thinking-like behavior (in which case it is the software that is doing the 'thinking', not the computer).
Setting up the Programming Environment
Programming requires certain specialized tools most of which are specific to the programming language being used. While some of these tools may come pre-installed in your computer, often you need to install and configure them yourself.
Some options for setting up the Python programming environment
Option 1. use an online python programming environment
In the initial stages you can use an online programming environment to practice Python.
An example online Python programming tool you can use is repl.it:
- Go to repl.it and signup for a free account
- Go to https://repl.it/languages/python3 (or choose Python 3 from the options in the web page) to enter the online Python environment
Option 2. install python on your computer
We recommend installing the latest Python 3.*
version (not the 2.*
version)
Programming in Java requires minimally the Java Development Kit (JDK) to be installed in your computer. The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform.
Working with an Interactive Shell
Some programming environments provide an . In the python interactive shell you can type in a python expression and get the shell to evaluate the expression.
>>> 2 + 3
5
>>> 2 * 4 * 3
24
>>>
Operators
An expression normally contains operators that combine with values. Here are some common operators used in Python:
Operator | Operation | Example | Evaluates to... |
---|---|---|---|
+ | Addition | 2 + 2 | 4 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 3 * 5 | 15 |
/ | Division | 22 / 8 | 2.75 |
** | Exponent | 2 ** 3 | 8 |
% | Modulus/remainder | 22 % 8 | 6 |
// | Integer division | 22 // 8 | 2 |
When there are multiple operators in an expression, normal operator precedence apply. You can force a different precedence using parentheses.
>>> 2 + 4 * 5
22
>>> (2 + 4) * 5
30
Data Types
A data value has a type. Here is a summary of three basic data types used in Python often:
Type name | Description | Examples |
---|---|---|
int | whole numbers (integers) | -23 , -22 , -1 , 0 , 1 , 65 , 66 |
float | numbers with decimal points (floating point numbers) | -34.576 , 436.0 |
str | sequence of characters (strings) | adam What can it be? , x |
String values are denoted by enclosing them in double quotes or single quotes.
'Hello'
"What's going on?"
Some operators can be used on strings as well.
>>> 'hello'
'hello'
>>> 'Hello' + ' ' + 'World!'
'Hello World!'
>>> 'Bye' + ('!' * 10)
'Bye!!!!!!!!!!'
Variables
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program. --https://launchschool.com/books/ruby/read/variables (emphasis added)
The following statement a variable named v
and assigns it a value 5
(the operator =
is called the assignment operator):
v = 5
Statements
Note that we called v = 5
a statement. Unlike expressions that evaluates to a single value, a statement does not evaluates to a value. However, a statement can contain an expression e.g. v = 5 + 8
.
If you enter the above statement in the interactive shell, it causes the shell to remember the variable name v
and its value.
>>> v = 5
>>>
Now, you can ask the shell to recall the value by simply typing in the variable name as an expression.
>>> v
5
You can even use the variable in expressions.
>>> v * 4
20
Here are some examples of variables being declared, assigned, and being used:
>>> max_bag_count = 2
>>> max_bag_count
2
>>> price = 10.0
>>> discount_percentage = 5
>>> discount = price * discount_percentage / 100
>>> price - discount
9.5
Python variable names (by convention) use lower case letters with _
separating words.
long_variable_name = 56
Variable names are case sensitive. In the example below, you can see how Python prints an error message if you try to access the variable height
using the name Height
>>> height = 12.0
>>> height
12.0
>>> Height
Traceback (most recent call last):
File "< pyshell#5 >", line 1, in < module >
Height
NameError: name 'Height' is not defined
Variables can be overwritten. In the example below, height
was initially 5
but the value is reassigned later to be 1
more than the previous value.
>>> height = 5
>>> height
5
>>> height = height + 1
>>> height
6
The value of one variable can be assigned to another variable.
In the example below, the value of variable height
is assigned to the variable width
.
>>> height = 5
>>> width = height
>>> width
5
>>> height + width
10
>>> height = 10
>>> width
5