Programming Fundamentals Lab 01
Programming Fundamentals Lab 01
Experiment No. 01
Lab 01 – Introduction to Programming Fundamentals .
Lab Objectives:
1. Introduction to Programming
2. Introduction to Anaconda Framework
3. Introduction to High Level Programming
4. Familiarization with different IDEs
Introduction to Programming
1. Background:
Communicating with a computer involves speaking the language that computer understands,
which immediately rules out English as the language of communication with computer. The
classical method of learning English is to first learn the alphabets used in the language, then learn
to combine these alphabets to form words. Instead of straight-away learning how to write
programs, we must first know what alphabets, numbers and special symbols are used in C, then
how using them constants, variables and keywords are constructed, and finally how are these
combined to form an instruction. A group of instructions would be combined later on to form a
program. This is illustrated in the Figure 1.1.
Alphabets, Constants,
Digits, Variables, Instruction Programs
Special Key Words
Symbols
The distribution comes with more than 1,000 data packages as well as the Conda package and
virtual environment manager, so it eliminates the need to learn to install each library
independently. As Anaconda's website says, "The Python and R conda packages in the Anaconda
Repository are curated and compiled in our secure environment so you get optimized binaries that
'just work' on your system."
As you know Anaconda Navigator, a desktop graphical user interface (GUI) system that includes
links to all the applications included with the distribution including RStudio, iPython, Jupyter
Notebook, JupyterLab, Spyder, Glue, and Orange. The default environment is Python 3.6, but you
can also easily install Python 3.5, Python 2.7, or R. The documentation is incredibly detailed and
there is an excellent community of users for additional support.
To work in Anaconda Distribution Environment. Click on Windows Icon -> Search for Anaconda3
-> Click on Anaconda Navigator.
After you click on the Anaconda Navigator it will take some time depending on your computer
configuration and then open the Anaconda Navigator Windows as shown in figure
Here we can see that Anaconda Distribution is full of tools such as Jupyter Notebook, Orange 3
,rstudio, vscode, sypder etc. All these tools will be use later in your upcoming semesters. But for
you in current semester we will use Jupyter Notebook. So let's start with Jupyter Notebook.
The Jupyter project is the successor to the earlier IPython Notebook, which was first published as
a prototype in 2010. Although it is possible to use many different programming languages within
Jupyter Notebooks, this article will focus on Python as it is the most common use case.
Figure 4 - Jupyter
This isn't a notebook just yet, but don't panic! There's not much to it. This is the Notebook
Dashboard, specifically designed for managing your Jupyter Notebooks. Think of it as the
launchpad for exploring, editing and creating your notebooks.
Be aware that the dashboard will give you access only to the files and sub-folders contained within
Jupyter's start-up directory; however, the start-up directory can be changed. It is also possible to
start the dashboard on any system via the command prompt (or terminal on Unix systems) by
entering the command jupyter notebook; in this case, the current working directory will be the
start-up directory.
The astute reader may have noticed that the URL for the dashboard is something like
http://localhost:8888/tree. Localhost is not a website, but indicates that the content is being served
from your local machine: your own computer. Jupyter's Notebooks and dashboard are web apps,
and Jupyter starts up a local Python server to serve these apps to your web browser, making it
essentially platform independent and opening the door to easier sharing on the web.
Prepared By: Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 4
October 10, 2018 Lab 01 – Introduction to Programming Fundamentals .
The dashboard's interface is mostly self-explanatory — though we will come back to it briefly
later. So what are we waiting for? Browse to the folder in which you would like to create your first
notebook, click the "New" drop-down button in the top-right and select "Python 3" (or the version
of your choice).
Hey presto, here we are! Your first Jupyter Notebook will open in new tab — each notebook uses
its own tab because you can open multiple notebooks simultaneously. If you switch back to the
dashboard, you will see the new file Untitled.ipynb and you should see some green text that tells
you your notebook is running.
There are two fairly prominent terms that you should notice, which are probably new to you: cells
and kernels are key both to understanding Jupyter and to what makes it more than just a word
processor. Fortunately, these concepts are not difficult to understand.
A kernel is a "computational engine" that executes the code contained in a notebook document.
A cell is a container for text to be displayed in the notebook or code to be executed by the
notebook's kernel.
3.6 Cells
We'll return to kernels a little later, but first let's come to grips with cells. Cells form the body of a
notebook. In the screenshot of a new notebook in the section above, that box with the green outline
is an empty cell. There are two main cell types that we will cover:
A code cell contains code to be executed in the kernel and displays its output below.
The first cell in a new notebook is always a code cell. Let's test it out with a classic hello world
example. Type print('Hello World!') into the cell and click the run button Notebook Run Button in
the toolbar above or press Ctrl + Enter. The result should look like this:
When you ran the cell, its output will have been displayed below and the label to its left will have
changed from In [ ] to In [1]. The output of a code cell also forms part of the document, which is
why you can see it in this article. You can always tell the difference between code and Markdown
cells because code cells have that label on the left and Markdown cells do not. The "In" part of the
label is simply short for "Input," while the label number indicates when the cell was executed on
the kernel — in this case the cell was executed first. Run the cell again and the label will change
to In [2] because now the cell was the second to be run on the kernel. It will become clearer why
this is so useful later on when we take a closer look at kernels.
So far we have seen how to run a cell with Ctrl + Enter, but there are plenty more. Keyboard
shortcuts are a very popular aspect of the Jupyter environment because they facilitate a speedy
cell-based workflow. Many of these are actions you can carry out on the active cell when it's in
command mode.
Below, you'll find a list of some of Jupyter's keyboard shortcuts. You're not expected to pick them
up immediately, but the list should give you a good idea of what's possible.
Toggle between edit and command mode with Esc and Enter, respectively.
Scroll up and down your cells with your Up and Down keys.
Ctrl + Shift + -, in edit mode, will split the active cell at the cursor.
You can also click and Shift + Click in the margin to the left of your cells to select them.
A flowchart is a graphical representation of an algorithm. These flowcharts play a vital role in the
programming of a problem and are quite helpful in understanding the logic of complicated and lengthy
problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language.
Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say
that a flowchart is a must for the better documentation of a complex program.
a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order.
b. The flowchart should be clear, neat and easy to follow. There should not be any room for
ambiguity in understanding the flowchart.
c. The usual direction of the flow of a procedure or system is from left to right or top to bottom.
d. Only one flow line should come out from a process symbol.
or
e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each
possible answer, should leave the decision symbol.
h. If the flowchart becomes complex, it is better to use connector symbols to reduce the number
of flow lines. Avoid the intersection of flow lines if you want to make it more effective and
better way of communication.
i. Ensure that the flowchart has a logical start and finish.
j. It is useful to test the validity of the flowchart by passing through it with a simple test data.
Algorithm:
Steps:
1. input x
2. input y
3. sum = x + y
4. average = sum /2
5. output average
START
Input x
Input y
Sum = x + y
Average = sum/2
Output
Average
END
Hints:
Problem 3: Write a program that asks the user to enter their name and their age. Print out a
message addressed to them that tells them the year that they will turn 100 years old.
Problem 4: Write a Python program to print the following string in a specific format
print("Twinkle, twinkle, little star, \n\tHow I wonder what you are! \n\t\tUp above the world so
high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star, \n\tHow I wonder what you
are!")
Problem 5: Write a Python program to display the current date and time.
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
Problem 6: Write a Python program which accepts the radius of a circle from the user and
compute the area.
Problem 7: Write a Python program which accepts the user's first and last name and print them
in reverse order with a space between them.
Problem 8: Write a Python program to print the calendar of a given month and year.
Note: Use 'calendar' module.
import calendar
y = int(input("Input the year : "))
m = int(input("Input the month : "))
print(calendar.month(y, m))
Programming Exercise