Programming topics allocated to the week will appear in this tab.
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:
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:
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.
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
>>>
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
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 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
While the interactive shell is good for testing out small code bits, longer Python programs (also called scripts) are written as text files with the .py
extension. Python reads the program and executes statements from top to bottom.
Let's create and run a Python program:
Press the button to run the code (don't worry about the meaning of the code). You can edit the code and run again!
File
→ New File
print('Hello world!')
print('Bye!')
File
→ Save as
. You can give a name such as hello.py
Run
→ Run Module (F5)
. The output will show up in the IDLE shell.You can use comments to provide more information about the code to the reader. Any text that follows #
(until the end of the line) is considered as a comment by the Python interpreter, and is ignored.
# this is a simple hello world program
print('Hello') # start with a greeting
# read user's name from the keyboard
print('What is your name?')
name = input()
Blank lines too are ignored by the Python interpreter. You can use blank lines to improve the of the code e.g., by using a blank line to separate one group of statements from another
A function is like a small program that you can execute from another program. A function has a name. Some functions take inputs (called arguments). The statement to execute (other names: call, invoke) a function takes the form function_name(arguments)
e.g., foo(1, 'hello')
calls a function named foo
with arguments 1
and 'hello'
Python comes with many built-in functions you can use right away.
To print some text, you can use the print
function which can take one or more arguments:
>>> print('Hello World')
Hello World
>>> print('Bye', 'my', 'friend')
Bye my friend
print('Hello World')
print('Bye', 'my', 'friend')
Hello World
Bye my friend
Some functions return a value.
int
, float
, str
functions can be used to convert a data value of one type to another. The built-in function type
tells you the type of a value.
age = 25 # age is of integer type
print('My age is ' + str(age)) # convert age to a string before printing
print(type(-4)) # print the type of value -4
print(type('-4')) # print the type of value '-4'
print(type(float('5.0'))) # convert string '5.0' to a float and print the type
My age is 25
<class 'int'>
<class 'str'>
<class 'float'>
input
function can be used to read input from the keyboard. It waits for user input (until the user hits Enter) and returns all text entered by the user as a string. Note how a call to the print
without any arguments (i.e. print()
) prints an empty line.
# read current price
print('What is the current price?')
price = input() # price is of type string because input() always return a string
# print current price
print() # print an empty line
print('Current price is $' + price) # no need to convert; price is already a string
# calculate and show discounted price
discounted_price = float(price)*0.5 # convert to float type first
print('Price after 50% discount is $' + str(discounted_price)) # convert back to string