0% found this document useful (0 votes)
10 views1 page

Computer Science 1001 - Notes

This document provides an introduction to Python, highlighting its features such as simplicity, readability, and versatility for various applications. It outlines the building blocks of Python programming, including data types, operations, statements, and the importance of indentation in code structure. Additionally, it emphasizes the significance of algorithms in programming and the role of Python's standard library in facilitating efficient coding practices.

Uploaded by

atefzohaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Computer Science 1001 - Notes

This document provides an introduction to Python, highlighting its features such as simplicity, readability, and versatility for various applications. It outlines the building blocks of Python programming, including data types, operations, statements, and the importance of indentation in code structure. Additionally, it emphasizes the significance of algorithms in programming and the role of Python's standard library in facilitating efficient coding practices.

Uploaded by

atefzohaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Computer Science 1001

Unit 2 - Topic 1: Introduction to Python

Show All Examples Hide All Examples

Quotes

"My favorite language for maintainability is Python. It has simple, clean syntax, object encapsulation, good library
support, and optional named parameters." - Bram Cohen

Python is the "most powerful language you can still read". - Paul Dubois

Introduction

Consider a book and think about the basic elements of a book and how it is organised. In general, a book is composed
of chapters which are composed of sections which are composed of paragraphs which are composed of sentences which
are composed of words which are composed of characters. Besides these basic building blocks of the book, the
grammar of the language in which the book is written is also very important. If the sentences of the language are
grammatically incorrect or the meaning of the words used does not convey the actual meaning to be conveyed, then
the book becomes useless. The same scenario is true about a program coded using a programming language. A
program is composed of modules which are composed of statements which are composed of expressions which are
composed of operators and operands. Operators manipulate operands which are the data values and objects.

Objectives

To get a general overview of the programming language Python and its building blocks

Data and Data Types


Data Processing

To gain an understanding of the building blocks for processing data

Operands and Operations


Python's Standard Library
Built-in Functions
Statements
Expressions
Flow of Control

To gain an understanding of the structure/layout of a Python program.

Overview of Python

Programming is the process of taking an algorithm and coding it as a program using a programming language so that
it can be executed by a computer. Although many programming languages and many different types of computers exist,
the important first step is to have the algorithm that solves the given problem. Without an algorithm, there can be no
program.

In this unit, we will get an overview of the programming language Python.

Python was created by Guido van Rossum, a Dutch programmer. Python was first released in 1991. It is named after
the BBC show "Monty Python's Flying Circus" and has nothing to do with reptiles.

Python is

a beginner-friendly language which is quick and easy to learn, quick and easy to use and fun to work with. In a
very short time you can code programs using Python. It handles a lot of complexity for you, hence it allows
beginners to focus on learning programming concepts and not have to worry about too much detail.
easy to understand. Being a very high level language, Python reads like English and hence is easy to code in and
easy to understand the coded programs.
a very general purpose language which provides the basic building blocks that can be used to build just about
anything, such as games, desktop apps, software for scientific computing, data analysis, artificial intelligence, web
development, etc.
open source. The source code for Python itself is freely available and anyone may suggest changes. The evolution
of the language is now guided by the Python Software Foundation.

Building Blocks of Python

Algorithms describe the solution to a problem in terms of the data needed to represent the problem and the set of
processing steps necessary to produce the intended result. Python, like all other programming languages, provides a
notational way to represent both; the data and the processing steps.

Data and Data Types

Data values within a program can be used directly as literal values or stored in variables that have a data type
supported by the programming language (or a data type defined by a user).

Literal values are exact data values, such as 5, 0.15, "Hi There", that can be used in a program.

Variables are named storage locations in a computer's memory that has a name to identify it and holds some data
value(s).

Data types determine how data values are represented in memory and the operations that be performed on those
values. Remember that a computer is an electronic device thus, everything inside the computer is represented as
strings of binary digits (0's and 1's) where 0 represents an off switch and 1 represents an on switch. But what do these
strings of 0's and 1's mean? Data types provide an interpretation for the binary string for stored data values so that we
can use the stored data appropriately. For example, most programming languages provide a data type for integers so
that strings of binary digits in the computer's memory can be interpreted as integers and given the typical meanings
that we commonly associate with integers (e.g. 23, 654, and -19). Python has two main built-in numeric classes that
implement the integer and floating point data types and a class of string which implements the text string or
alphanumeric data type. Each data type has operations defined specifically for that data type. Thus, a data type defines
a set of data values and the operations allowed on those values.

In Python, every datum is an object. An object is defined by a class which includes the attributes or characteristics of
the object and the methods that describe the actions that can be performed on the object.

Several objects of various types can be combined together to form more complex data structures, as we will learn
about in later units.

Building Blocks for Processing Data

Operands and Operations

Primitive operations are operations, such as addition, subtraction, multiplication, etc., that are directly available to be
used in the program to manipulate data. Each data type has its own set of primitive operations that defines how the
data type can be manipulated.

Python's Standard Library

The Python standard library is a collection of modules which in turn are a collection of related functions and methods
that provide standardized solutions for performing many tasks that occur in everyday programming. For example, if you
wish to draw a figure you can spend hours to see how you would write code to draw the desired figure or you could use
the turtle module which provides the object type turtle and the methods that manipulate the turtle using Python's
built-in graphics module for drawing lines, circles and other shapes.

Library modules help prevent programmers from reinventing the wheel and allow for reuse of code to do the job at
hand. Most library modules must be imported into a piece of Python code before they can be used.

Built-in Functions

Computations that cannot be performed in one step using the primitive operators, but are used quite frequently by
programmers, are coded as programs and provided within the Python interpreter as functions. These are always
available and can be used directly in program code. For example, the min() function returns the minimum value of the
list of data values enclosed within the parenthesis after the function name.

Functions are like black boxes which, when supplied the required input values (called arguments), return a value(s)
after processing. We don't need to know how the processing is done necessarily. We need to only know the name of the
function, what it can do and what arguments it needs.

Statements

A statement is an instruction coded, in the programming language used, to describe the action to be performed.
Different types of statements are as follows:

Comment statements which document the program and make it easy to understand what the program is doing.
Assignment statements which dynamically change the values of the variables.
Input statements which interactively obtain a value for a variable, i.e., as the program is running.
Output statements which output the value of a variable and/or literals.

Statements may include expressions.

Expressions

Expressions are used to perform computations. The simplest of all expressions is a single variable or a data value. The
data values and the operations defined on the data types of the data values are combined to compute new values.
Expressions include operands (functions, variables and/or literals) and operators (such as +, -, etc.).

Flow of Control

Flow of control refers to the order or sequence in which the statements of the program are executed. Common flow of
control structures are sequential, decisionstructures, repetition structures and function calls.

Programming Style

Indentation matters for Python. The statements of a sequential Python program should be entered from the first
column in each new line. The following will generate an error:

print("This statement starts in column 1")


print("This statements starts in column 5")

Both the statements should start from column 1.

Python uses indentation to mark blocks of code. Python is sensitive to spaces left before typing a statement unless it is
part of a flow of control structure. The following program has statements indented with respect to the while statement,
as a block.

#This program adds 10 given input values


sum = 0
numOfVal = 10
count = 1
while count <= numOfVal:
num = int(input("Enter number: "))
sum += num
count +=1
print("The sum of all the given numbers is:", sum)

Note: The amount of indentation matters! A missing or extra space in a Python block could cause an error or
unexpected behavior. Statements within the same block of code need to be indented at the same level.

Important Points
A Python program is a collection of statements, which includes expressions.
An expression is a collection of operands and operators.
Operands can be data values or function calls that return data values.
Operators are operations that are defined to manipulate data types.
Flow of control is the order in which statements within the program are executed.
Decision and repetition structures and function calls change the sequential flow of control with a program.
Indentation within a program matters.

Prepared By: © Radha Gupta, Department of Computer Science, Memorial University of Newfoundland
(BE170719)

You might also like