Bda24202 Chapter2
Bda24202 Chapter2
COMPUTER
PROGRAMMING
CHAPTER 2
The Structure of Programming Language
2.1 Introduction to Programming Structure
2.2 Basic Syntax and Structure (variables,
comments, indentation)
2.3 Data Types: primitive type & conversion
type
Introduction to Phyton
• Python is a general-purpose programming language in a similar to
other programming languages that you might have heard of such
as C++, JavaScript or Microsoft’s C# and Oracle’s Java.
• The easiest programming language used among developers is
Python.
• This language is open-source, enabling programmers to modify
its source code to fit their needs.
• It is an object-oriented programming language.
• The language has a high-level data structure, and built-in libraries,
that make it easy to use and suitable for rapid application
development.
• It can be used for various tasks, from data analysis and
visualization to web development, prototyping, and automation.
• It has been around for some considerable time having been
originally conceived back in the 1980s by Guido van Rossum at
Centrum Wiskunde & Informatica (CWI) in the Netherlands.
Introduction to Phyton
Term Definition
Open-source software (OSS) Open source means it is free. Phyton
has large community with access to the
software’s code to continuous
development and upgrading
General purpose There is a broad set of field of Phyton-
web programming, analysis of financial
data, analysis of big data and more
High-level Employs syntax closer to human logic,
make the languages easier to learn
Introduction to Phyton
There are several ways in which you can run a Python program, including:
Python interpreter
Download and Install Phyton IDE
7
INSTALLING
ANACONDA
8
Installing Jupyter Notebook and Spyder IDE
Use Anaconda Navigator
1. Select suitable environment
2. Scroll the applications and select icon ”Launh” for Jupyter Notebook & Spyder
environment
Installation of Relate Phyton IDE &
Libraries
OPTION 2
Create the environment using Anaconda Prompt
• Open the “Anaconda Prompt (Anaconda3)”
located on Windows Start menu.
OPTION 1
• To create an environment, type “conda create --
Creating the environment using
name myenv” inside the anaconda prompt and
Anaconda Navigator
proceed.
• Click “Create” in
• “myenv” can be replaced by own environment
Environments tab.
name.
Activating the environment
• Activation of conda environment by typing in
“conda activate myenv” in the anaconda prompt.
10
• Open Anaconda navigator
• Choose Environments → Create→ Name environment as
“myenv”→click create
Starting Python using Jupyter
• Open Anaconda navigator → Launch Jupyter
• Your default browser should start (or open a new tab) to the
following URL: http://localhost:8888/tree
• To execute a cell, select the cell and click the Run button
Starting Python using Spyder
• Open Anaconda navigator → Launch Spyder
Editor:
Write your script/
instruction here.
Will be executed to
the console Console:
Code execution
directly form script
Introduction to Programming Structure
Define variable
Statement
Python command
Modes of Python Interpreter
Writing coding
Output
Variables in Python
Command/Instruction
Output
Creating Variables
• Variables do not need to be declared with any particular type, and
can even change type after they have been set.
• Example:
greeting = "Hello"
planet = "World"
print(greeting, planet)
• The output is:
'Hello World'
• The variables are: ‘greeting’ and ‘planet’
• Any names
Python Data Types
• In programming, data type
is an important concept.
• Variables can store data
of different types, and
different types can do
different things.
• Python has the following
data types built-in by
default, in these
categories:
Example of Writing Data Types in Python
String
Integer
Float
Complex
List
Tuple
Range
Python Numbers
There are three numeric types in Python:
• int
• float
• complex
Python Strings
• Strings in python are surrounded by either single quotation marks,
or double quotation marks.
• Can use quotes inside a string, as long as they don't match the
quotes surrounding the string
Assign String to a Variable
• Assigning a string to a variable is done with the variable name
followed by an equal sign and the string:
• Notice:
• The message is complex – read it carefully
• Only the first error is mentioned
Exercise 1
• Write the following command in Spyder Editor and
observe the output and data type in Variable Explorer
Exercise 1: Solution
• Write the following command in Spyder Editor and observe the
output and data type in Variable Explorer
Editor
Output
Exercise 2
• What is the output for this coding?
Exercise 2:Solution
• What is the output for this coding?
Assignment
• The statement to set a variable is call ‘assignment’
>>> x = 10
• … can be read as ‘10 is assigned to x’
>>> x = y
• When it sees ''', it scans for the next ''' and ignores any text
between the triple quotation marks.
• Here are examples of comments:
• Note that the statements are entered from the first column in the new
line. It would cause an error if the program is typed as follows:
• For example, the Python interpreter will report errors for the following
code:
• It would be wrong, for example, to replace print in the program with Print.