Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
of Contents
Overview
1 First Program
2 Variables
3 Expressions
4 Conditions
5 Loops
6 Functions
7 List
8 Time
9 Files
10 Database
Overview
Python is a great language for gamers and programmers. This book is for those
who are new to this language and such the book will go through the
fundamentals of writing a program using Python.
The book starts with writing a basic program and goes through the normal
sections of a program such variables, conditions, loops, lists, functions, files and
database elements.
Prior knowledge of programming in any language will be helpful but really not
required.
Before we start:
The first thing to do is to get an editor for writing the programs. There are two
ways to get an editor. One an online editor and the other to install the software.
The second option is recommended because to understand a language it is best to
install the software or editor that goes with it. Online editors have limitations
depending upon the provider and does require the programmer to be online.
The editor to install is available at http://www.python.org and the version this
book uses 3.6.3 even though python does release new versions comparatively
frequently.
Install the version appropriate for computer. The examples in this book were
done on a Windows 10 64-bit machine.
Download and install the version for your computer. Note there are two versions
(64 bit and 32 bit) for Windows machines.
Installation can be the suggested default directories.
The best way to learn programming is through pencil and paper before typing
into an editor. It is an old way of learning in a time of tablets and touch screens
but it has proven to always work.
The website for this book is http://www.beam.to/fjtbooks or type fjt books in
google search.
Chapter 1. First Program
The first program written in every programming class is one to display the words
"Hello World". It is an unwritten tradition. In this regard start the Python editor
from windows apps.
The editor can be started with by clicking the IDLE Python option as shown in
the above picture. The editor looks like a notepad file with a menu on top.
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit
(AMD64)] on win32
>>> greetings ="Hello World!!"
>>> print(greetings)
Hello World!!
>>> print("Hello World")
Hello World
In the code above there are two ways to print Hello World to the screen. The first
is to start with a variable called greetings which has the string value of "Hello
World" followed by a print statement that prints the values of the variable. Note
the brackets in the print statement.
The second method is a direct print statement where the string is inside the
brackets within double quotes.
Thus the tradition of printing Hello World has been completed using Python.
This method will work irrespective of the version of python interpreter
downloaded.
The above code was written directly into the editor a line at a time. Suppose an
entire code (two or more lines at a time) has to be written before running the
execute command.
In this case go to File in the editor and click on New File. This will open a
notepad like page. Write the entire code here and hit Run. Choose F5 Run. The
editor will prompt for the program to be saved.
The next step in the process of making a Python program is to save the program
written.
The default location will come up. This location can be chosen or any other
folder on the computer.
The next time the same code is to be run, open the editor and choose open under
File option on the menu and choose the file by its name.
It is advisable to use the default location until one becomes more conversant
with the editor and the Python language.
The editor allows for code to copied and pasted into other documents especially
word documents. This makes it easier for those working on projects.
Never forget to save changes when modifying code. It is best to get the habit of
saving frequently as that would save a lot of pain in the long run. This is from
experience.
The editor configuration such as font used, highlights, indentation used as
standard can be changed by going under the options menu above.
Generally, for starters such a change is not required.
Workshop:
1. Print the words "My First Python Program" using a variable.
2. Print the words "My First Python Program" using a direct print statement.
3. Note and write down the version of Python editor on the machine you are
using.
4. Python uses a compiler to compile programs - True / False.
5. Python can be used for all types of programs just like any other programming
language. True / False
6. The extension of a python file is _____.
7. What is the difference between a compiler and an interpreter?
X-X-X
Chapter 2. Variables
Variables in programming languages are considered temporary storages of data.
It is common to use the alphabets as variables but more meaningful names will
greatly help the programmer in the long run.
Variables can have data types. The usual data types are:
Integer
Float
Long
Double
String
Array
Boolean- True/False
Python is no different in having variables but what makes it stand out is that
these variables can be stated dynamically.
In a programming language such as Java, C++ etc. a common way to introduce a
variable would be as follows.
Int x
x = 2;
Here in Python it will be written as follows.
x= 2
x= "Hello"
x= False
x= True
x= 1.5
Notice that the use of double quotes makes the value a variable a string. Thus x
= 2 and x ="2" are not the same.
Numbers whether integer or double can be printed to the screen as follows:
print (4)
print (4.54)
The printing of the string to the screen was seen in the previous chapter.
print (False)
print (True)
Note that capital letters for the Boolean values.
In built into python the int function can convert a string representation to an
integer number format and str function can convert a number into string format.
Example:
>>>str(4)
>>> '4'
>>> int('4')
>>> 4
Not all strings have integer equivalents.
For numbers the basic operations on them are:
+ for addition
- for subtraction
/ for divide
* for multiplication
** for exponent
Example:
>>> 4**4
>>> 256
>>> 2/2
>>> 1.0
But for a string the plus (+) sign means concatenate.
Example:
>>>"ABC" + "123"
>>> 'ABC123'
>>> '5' + '4'
>>>'54'
>>> int('5') + int('4')
>>> 9
>>>'5' + str(4)
>>>'54'
The type function can determine the type of an expression.
Example:
>>> type (3 +4)
>>><class 'int'>
>>>type ('3' + '4')
>>><class 'str'>
No commas can be used with numbers in Python. For example 4,15 cannot be
used but 415 is okay.
Python integers can be as large as possible. The memory of the computer sets the
limits of the size of the integers.
As seen in the last chapter variables can be assigned values either numbers or
strings or Boolean.
The assignment operator is the equal to (=).
The assignment statement is read such that the variable on the left hand side of
the equation (=) takes on the value or the value of the variable on the right hand
side.
x= y
means x takes on the value of y.
Example:
>>>x = 5
>>>print(x)
>>> 5
>>>print('x')
>>> x
>>> print("x is:" + str(x))
>>> x is: 5
x is an integer but the string to concatenate needs a string variable. Thus in the
above last line x was converted to string for concatenation.
To avoid this and print variables of different types on a single line use a comma
separated list as follows:
>>> print ("x is:" , x)
>>> x is: 5
Such a comma separated list of variables is referred to as a tuple assignment.
>>> x =0
>>> y= "Hello"
>>> z = 45
>>> print (x,y,z)
>>> 0 Hello 45
The value and type of a variable can change within a program.
Example:
>>>x=10
>>>print (x)
>>>10
>>>x="ABC"
>>>print (x)
>>> ABC
Alphabets can be good variable names but when programs get bigger it will
become confusing. Thus descriptive names such as total, sum etc can take the
place of alphabets as variables.
Python has reserved words that cannot be used as variable names just like other
programming languages.
and del from None try
as elif global nonlocal True
assert else if not while
break except import or with
class False in pass yield
continue finally is raise
def for lambda return
are examples of reserved words in Python. These cannot be used as variables.
In addition to these reserved words the following rules must be followed when
choosing variable names.
1. A variable must contain at least one character.
2. The first character must be an alphabetical character of upper or lower case or
an underscore.
3. The remaining must be alphabetical characters of upper or lower case or digits
or
underscore.
4. Spaces are not acceptable as identifiers.
Floating point numbers are those numbers whose decimals points can move or
float depending on the calculations on them.
Example:
>>>x = 3.45
>>>x= x *20
>>>print (x)
>>> 69.0
The characters that can appear within strings include letters of the alphabet (A-Z,
a-z), digits (0-9), punctuation (., :, ,, etc.), and other printable symbols (#, &, %,
etc.).
Control codes control the way text is rendered in a console window or paper
printer. The backslash symbol (\) signifies that the character that follows it is a
control code, not a literal character.
The \n control code represents the newline control code which moves the text
cursor down to the next line in the console window.
Other control codes include \t for tab, \f for a form feed (or page eject) on a
printer, \b for backspace, and \a for alert (or bell).
Listing 2.8: specialchars.py
Example:
>>>print("A\nB")
A
B
>>>print ("A\tB")
A B
>>>print ("A\bB")
A
A string can be included within a double or single quote. If a double quote is
used, then single code can be included within it and vice versa. The choice is up
to the programmer.
Since the backlash is used as a control code a double backlash (\\) has to be used
if trying to write a path or web link
>>> x= ("C:\\User\\Users\\Desktop")
>>> print("Print file location is:" + x)
>>> Print file location is: C:\\User\\Users\\Desktop
Up to now the values of various variables used in the examples has been hard
coded. This is not practical all the time. If the Python program needs an input
from outside the program such as from the keyword the input function can be
used.
Example:
print("Please enter a number")
x=input()
print(x)
print(type(x))
Now run the program and one will the type is given as string.
The input function will give only strings. Use the int function to convert the
input into number if needed.
Example:
print("Please enter a number")
x=input()
print(x)
print(type(x))
x=int(x)
print(type(x))
x= x+1
print(x)
This will give the output as follows:
Please enter a number
1
1
<class 'str'>
<class 'int'>
2
The above program can be modified to include the prompt within the input()
function as follows:
x=input("Please enter a number")
print(x)
print(type(x))
x=int(x)
print(type(x))
x= x+1
print(x)
Like the int function the float function can be used to convert the input into a
float. Python provides the eval function that attempts to evaluate a string in the
same way that the interactive shell would evaluate it.
Example:
x1 = eval(input('Entry x1? '))
print('x1 =', x1, ' type:', type(x1))
Run the code and enter various types of values-integer, float or string.
Example:
print('A', end='')
print('B', end='')
print('C', end='')
print()
print('X')
print('Y')
print('Z')
The output will be as follows:
ABC
X
Y
Z
The print statement accepts an additional argument with the end='' that allows
the cursor to remain on the same line as the printed text.
The string x= "Hello World" can be represented by indices.
x[0] = H
x[1] = e
x[2] = l
and so forth. This feature of Python and string can prove very useful.
x= "Hello World"
print (len(x))
This will give the length of the string which is 11 including the space.
Workshop:
1. Write a program the will prompt the user to give his or her name and give
back the length of the name.
2. Will the following lines of code print the same thing? Explain why or why
not.
x = 10
print(x)
print("x")
3. Which of the following are not correct variable names in Python?
(a) John
(b) if
(c) 2x
(d) 15+
(e) sum_total
(f) x2
(g) Private
(h) _pass
(i) pa sword123
(j) +_if
(k) a_none
4. Write a program in Python that will take two numbers and multiply them. Use
the input method and convert into float.
5. Write a Python program that will give the following output.
A
B
C
XY
12
10 11 12
6. How is definition of variables in Python different from that in other languages
such as C++, or Java etc?
7. Is the following operation allowed in Python?
x= 5
x= x + 5
z= 1
y= z + x
x =y
Chapter 3. Expressions
The term expressions can be best associated with mathematics. These are used to
manipulate inputs for a certain objective.
Look at the following expressions in Python.
a + b means a is added to b if both are numbers. If they are string, then a and b
are concatenated.
a - b means a take away b if both are numbers.
a * b means a multiplication between two numbers if both are numbers.
However, if a is a string and b is a number then it means that a will be
concatenated with itself b times. If b is a string and a is a number, then b will be
concatenated with itself a times.
a / b means a divided by b if both a and b are numbers.
a //b means floor of a divided by b if both a and b are numbers. Gives a truncated
number. For example, 10//3 will give 3 and 5//10 will give 0.
a % b means remainder of the division by b if both a and b are numbers. This
may be referred to as mod. For example, 10%3 will give 1.
a ** b means exponent or a raised to the power of b. Like 23 .
In math there is a precedence order. Precedence is important when an expression
has a mixed set of mathematical operations and the question arises which should
go first.
2 + 5 * 4
This can be interpreted in two ways.
2 + 5 = 7
7 * 4 = 28
or
2 + 20 = 22
The two results are different.
The multiplicative operators (*, \, %,\\,**) have equal with each other and the
additive operators (+, -) have equal precedence with each other.
The multiplicative operators have higher precedence than additive operators.
The brackets are used to over ride precedence order. Therefore, a programmer
should note this when making expressions.
Binary operators (+, -, *, \, %,\\,**) are left associative while assignment
operator is right associative.
2 -5 -7
All are the same operators above. So do we start with the second subtraction or
the first? The operator being left associative the first subtraction is done first.
-3 -7 = -10
Brackets can again make a difference.
x = y = z
means give the value of z to y and then give the value of y to x.
In python comments are placed by the # sign.
# This is my first Python program
print ("Hello World")
It is very important to comment written code in any programming language. The
importance will be realized when programs become longer. Comments often
remind the programmer why a particular way of programming was used when
the program was initially written. Useful comments might give the programmer
a window into the algorithm used to make the program. This is very useful if the
programmer is looking at a code after a long time.
Errors
There are three types of errors that can take place when writing a program. This
is not specific to Python but to all programming languages.
Syntax errors - These are due typing mistakes or missing of a colon or a bracket
or a quote etc.
Run Time errors - These occur when the program runs. The program may not
have any syntax errors yet, run time errors can happen.
For example:
x= y * 2
but value of y has not been assigned.
Logical errors - These errors occur when the logic or algorithm intended has not
been properly programmed. The program may run without any errors but the
answer given might be wrong.
These errors are very important because ultimately every program bought or sold
depends on a reliable algorithm that is expected to give the correct result or
perform in accordance with specification.
Logical errors are harder to find than the two previous type of errors. It is best
for an independent programmer to check out the logic in a project once the
programmer submits his or her program into the test phase.
Division by 0 is a classical example of a logical error.
Increment and other Operators
In Python an increment can be done as follows:
x= x + 1
or
x+ = 1
x = x -1
or
x- = 1
x = x +5
or
x+ = 5
x = x * 5
or
x* = 5
x= x /5
or
x/ = 5
x = x * (y+z)
or
x * = (y+z)
Algorithm
The term algorithm was used when defining the errors earlier in the chapter. An
algorithm is the basis of any program. It is defined as a finite sequence of steps,
each step taking a finite length of time, to solve a problem or compute a result.
Thus every programming challenge has to be broken down into its tiniest steps
that can be managed. Then the programmer has to program each step integrating
each step to get the final result.
This type of methodology (waterfall type) may be slow down development in an
age of agile programming and rapid application development. However, the
good old method does work.
Let us write a program that will accept two numbers from the keyboard and add
them.
Algorithm:
1. Write a program to accept input in float format.
2. Add the two numbers.
3. Output the result.
The program will look like the one below:
print ("This progam will take two numbers and add them and show the result")
x= float(input("Please enter the first number\t"))
y= float(input("Please enter the secont number\t"))
sum = x + y
print("Total is",sum)
The output will be as follows:
This progam will take two numbers and add them and show the result
Please enter the first number 10
Please enter the secont number 25
Total is 35.0
Note that using the float function the input has been converted into float as input
gives string values only. The sum variable takes the result of the addition. The
use \t at the end of each input statement makes it possible to keep the input value
at a tab space.
Modules
An important point when dealing with math in Python is to remember that
Python requires import of modules to do certain math or other functions. For
example- the square root of a number. The math module needs to be imported
using the import keyword. This is very similar to Java and C++ include
statements.
To find the modules available upon installation of the interpreter go to the
Windows Apps and click on Python 3.6 Module Docs.
This will bring up a page similar to this. Included in the first part of the page is
the list of in built modules for that particular version of the Python interpreter.
The math module can be seen in the list. Clicking on the module name will open
up the features that need that module.
The implementation is as follows:
import math
x= float(input("Please enter the first number\t"))
answer = math.sqrt(x)
print("Total is",answer)
The output will be as follows:
Please enter the first number 25
Total is 5.0
Workshop:
1. Write down an algorithm to convert kilometers into miles and miles using a
float input format.
2. Write down a program using the math module to find the greatest common
divisor of two input numbers. (Hint: format is integer, click on math module to
use gcd(x,y))
Chapter 4. Conditions
The If condition is the most used condition in most programming language.
Python is no different in this respect.
The following are assignment operators in Python.
x == y means check to see if x is equal to y
x < y means x less than y
x > y means x greater than y
x <= y means less than or equal to y
x >=y means greater than or equal to y
x!=y means x not equal y
The If statement in Python is as follows:
If (condition) :
statement or block of statements.
Note: There is no then keyword but a colon (:) . The colon is required.
If (x >10):
print ("The number entered is greater than 10")
The If statement can also have an else component. The format is as follows:
If (condition):
statement or block of statements
else:
statement or block of statements.
Note: There is a colon (:) after else. It is required. There can be more than one
else statement.
If (x >10):
print ("The number entered is greater than 10")
else:
print ("The number entered is less than 10")
Boolean stands for True or False. Boolean logical operators are:
And
OR
Not
In the And logical operator both conditions have to be true to get true result. In
OR only one condition has to be true to get a true result. Not will give the
negation of a condition.
In And:
True and True = True
True and False = False
False and True = False
False and False = False
In OR:
True or True = True
True or False = True
False or True = True
False or False = False
In Not:
Not True = False
Not False = True
Example:
In this example a number (integer) is being requested and checked if it is in a
particular range.
Note: The indentation in the code. This is required.
x =int(input("Please enter a number between 0 and 10\t"))
if ((x>=0) and (x<=10)):
print("The number is in range")
else:
print("The number is not in range")
The output will look like this:
Please enter a number between 0 and 10 5
The number is in range
Like in any programming languages if conditions can be nested if the program
requires it that way. Nesting means an if condition inside another if condition.
The flow and count of conditions need to be followed to avoid logical errors.
Example:
x =int(input("Please enter a number between 0 and 10\t"))
if ((x>=0) and (x<=10)):
print("The number is in range")
if (x%2==0):
print("The number is even")
else:
print("The number is odd")
else:
print("The number is not in range")
It is very important in the code above to observe the indentation. In Python this
is very important.
The else statement of the main If condition matches it in terms of starting
position. The inner else statement attached to the second if statement matches its
position with the second if. The same goes for the print indentation.
In the above program the first if condition checks to see if the number which is
an integer is in the 0 to 10 range. If not the program stops with an out of range
message.
If the number is in range of 0 to 10 then the number is checked for evenness by
finding the remainder of the number divided by 2. If it is 0 then it should be an
even number and otherwise odd.
It is possible to have an if statement after an else:
Example:
x =float(input("Please enter first exam mark between 0 and 100\t"))
y = float(input("Please enter second exam mark between 0 and 100\t"))
z = float(input("Please enter third exam mark between 0 and 100\t"))
w = float(input("Please enter attendance mark between 0 and 100\t"))
avg = (x* 0.2) + (y*0.3) + (z * 0.4) + (w *0.1)
if avg>=90:
print ("A")
else:
if avg >=85:
print ("B+")
else:
if avg >=80:
print ("B")
else:
if avg >=75:
print("C+")
else:
if avg >=70:
print("C")
else:
if avg>=65:
print("D+")
else:
if avg>=60:
print("D")
else:
print("F")
Please note the indentation in each condition. In this example three exam grades
are being requested and a final grade calculated and letter grade found. The code
really needs some more conditions in that the input numbers need to be checked
for range compliance.
A sample input and output will look as follows:
Please enter first exam mark between 0 and 100 90
Please enter second exam mark between 0 and 100 89
Please enter third exam mark between 0 and 100 93
Please enter attendance mark between 0 and 100100
A
Workshop:
1. In the example of computing grade in the previous page add the necessary
code to check if the grades entered is between 0 and 100 only.
2. Write a code that will ask the user to enter a Celsius or Fare height number
and convert it accordingly. (Hint: The first input question should be Degree
Celsius or Fare height )
3. Modify the question in the last chapter that converted kilometers to miles to
first request the unit and then perform the needed calculations. That is if
Kilometers convert to mile and if Miles convert to Kilometers.
4. Given the following definitions: b1, b2, b3, b4 = true, false, x == 5, y < 5
Evaluate the following Boolean expressions:
(c) not b1
(f) not b4
(g) b1 and b2
(h) b1 or b2
(l) b1 or b4
(m) b2 and b3
(p) b1 or b2 and b3
(q) b1 and b2 and b3
(t) not b1 or b2 or b3
(u) not (b1 and b2 and b3)
(y) not (not b1 and not b2 and not b3)
(z) not (not b1 or not b2 or not b3)
X-X-X
Chapter 5. Loops
A loop is used to repeat a set of statements. Let us start with the while
statement.
The format for the while statement is as follows:
while condition:
do statements
exit when condition is false.
Note that like the if statement the while condition has a colon (:). It is required.
Example:
count= 1
while count <=5:
count = count +1
print (count)
Note the indentation. Python editors are very particular about indentation.
The output will look like this.
2
3
4
5
6
Since the variable count started with 1 the output starts with 2 (1 +1) and ends
with 6(5+1).
Let us look at an example where we a password is being requested. Only three
chances are being given to give the correct password.
Example:
count=0
while count<=2:
count = count +1
x =input("Please enter password")
if x =="Admin":
print ("Password correct")
break
else:
print("Password incorrect")
Note the indentation. The other keyword here is break. The break stops the loop
and exists out of it.
The For Loop
The for loop works in a range. These can be numbers or others. For now, let us
look at the for loop with numbers.
for n in range (1,10,1)
print (n)
The first number is the starting point, and the second is the end point. The third
is the step. In this case steps of 1. This can be any other number and can be
negative also if counting backwards.
Let us rewrite the password example with a for loop.
Example:
for n in range(0, 3, 1):
x = input("Please enter password")
if x =="Admin":
print("Password ok")
break
Note the indentation. This is very important in Python and can cause new
programmers a little bit of inconvenience if not properly done.
Workshop:
1. Write a program to print the prime values from 0 to 10.
Chapter 6. Functions
Functions make programming simpler by reducing the amount of coded lines.
Python like most languages have in built functions.
We have been using some of them in the previous chapters. Print, input etc
Within each module there will be additional functions defined which can be used
by the programmer directly. An example of this was the sqrt function mentioned
earlier.
Some of other functions (not the exhaustive list) in the math module are:
exp- Computes e raised a power: exp(x) = ex
log- Computes the natural logarithm of a number: log(x) = loge x = ln x
log10- Computes the common logarithm of a number: log(x) = log10 x
cos- Computes the cosine of a value specified in radians: cos(x) = cos x; The
other trigonometric functions include sine, tangent, arc cosine, arc sine, arc
tangent, hyperbolic cosine, hyperbolic sine, and hyperbolic tangent
pow Raises one number to a power of another: pow(x,y) = xy
Check for more in built function by clicking on the module name as shown in
chapter 3.
Similarly if one checks out the time module the clock function can be found
which gives the actual CPU time
or real time since the start of the process or since
the first call to clock().
Example:
import time
x=input("Enter a value")
start = time.clock()
elapsed = time.clock() - start
print("You took", elapsed,"to respond")
Output
You took 3.84962739456448e-07 to respond
Similarly, if one looks at the random module in Python there are functions to
create random numbers.
Example:
import random
x= random.random()
print (x)
This will print a value of x in the range of 0 to 1 randomly.
Example:
import random
x= random.random()
x = x * 100
print (x)
This will print random numbers in the range of 0 to 100
Example:
import random
i=0
while i < 100:
x = random.random()* 100
i= i+1
print (x)
Will print 100 random numbers.
If the call to seed is left out, the initial value in the sequence is based on the
system’s time.
Example:
import random
i=0
random.seed(53)
while i < 100:
x = random.random()* 100
i= i+1
print (x)
The seed value determines the sequence of numbers generated; identical seed
values generate identical sequences.
It may be noted that the import statements used in all examples so far includes
the name of the module only.
Another way to import is as follows
From math import sqrt , log
or
From math import *
which means import every function in the math module.
The advantage with these two methods versus importing just the module is that
if one just imports the module then the module name. function name has to be
used within the code.
math.sqrt
instead of just sqrt.
The choice is up to the programmer.
A function is called by a program when needed. Code that will be repeated often
can be made as function to increase the efficiency of coding.
A function has:
a name
a body
and parameters.
If the list of parameters is not present, then the function does not expect any
information from the calling program.
Functions can be called by other functions.
The def keyword followed by the name of the function creates a function.
The simplest functions do not have any parameters and returns nothing.
Example:
def function1():
x=int(input("Please enter a value\t"))
return x
# start of program that will call the function
print("This program will add two numbers using a function")
x = function1()
y= function1()
sum = x+y
print (sum)
The value will be 0 for Monday. This code will give the day (Sun,Mon..) of a
particular date in a specific year.
By the way the choice of the date above was not arbitrary.
Example:
import time
cal = time.timezone
print(cal)
This will give the offset in seconds of the local time zone (without DST) from
UTC (>0 in the Americas; <=0 in most of Europe, Asia, Africa).
Example:
import time
cal = time.altzone
print(cal)
The offset of the local DST timezone, in seconds west of UTC, if one is defined.
This is negative if the local DST timezone is east of UTC (as in Western Europe,
including the UK). Only use this if daylight is nonzero.
Chapter 9. Files
So far the methods of input and output that was seen in the previous chapters
were print and input().
Python allows printing to the screen using the print function.
Example:
print("This is a print statement")
The input from the keyboard was obtained by using the input function.
x = input("Please enter a value")
This above assumes that x is expecting a string variable because by default input
only takes in a string.
The input can be changed to a int or float format by putting that command in
front of the input as follows:
x =int(input("Please enter a value")
for integer input
and
x = float(input("Please enter a value")
for float input.
Therefore , the keyboard and the screen has been used so far. Let us look into
files.