Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Getting Started Python Chapter 5 NEW 14-09-2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Chapter 5

Getting Started with Python


Introduction:
Python was created by Guido Van Rossum during his tenure at CWI (Centrum Wiskunde &
Informatica), a National Research Institute for Mathematics and Computer Science in the
Netherlands. The language was released in 1991 and was named after the BBC comedy
series from the seventies, “Monty Python’s Flying Circus.” Python supports both
procedural and object-oriented programming approaches and is free to use.

Features of Python:
 High-Level Language: Python is a high-level programming language that is free and
open-source.
 Interpreted Language: Python programs are executed by an interpreter, making it an
interpreted language.
 Readability: Python programs are easy to understand due to their clearly defined
syntax and relatively simple structure.
 Case Sensitivity: Python is case-sensitive, meaning that `NUMBER` and `number` are
treated as distinct identifiers.
 Portability: Python is portable and platform-independent, capable of running on
various operating systems and hardware platforms.
 Rich Standard Library: Python includes a rich library of predefined functions.
 Web Development: Python is useful in web development, with many popular web
services and applications built using the language.
 Indentation: Python uses indentation to define blocks and nested blocks, ensuring a
clean and readable code structure.
 Easy to use: It is easy to learn and use

Working with Python


To write and run a Python program, you need to have a Python interpreter installed on your
computer, or you can use an online Python interpreter. The interpreter is also referred to as
the Python shell. Below is an example of what a Python interpreter screen looks like (as
shown in Figure.

Figure 1 Python interpreter or shell

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 1


In the above screen, the symbol >>> is the Python prompt, which indicates that the interpreter
is ready to take instructions. We can type commands or statements on this prompt to execute
them using a Python interpreter.
Execution Modes There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual statement instantaneously. Whereas, Script
mode allows us to write more than one instruction in a file called Python source code file that
can be executed.
(A) Interactive Mode To work in the interactive mode, we can simply type a Python
statement on the >>> prompt directly. As soon as we press enter, the interpreter executes the
statement and displays the result(s), as shown in Figure.

Figure 2 Python interpreter in interactive mode

Working in the interactive mode is convenient for testing a single line code for instant
execution. But in the interactive mode, we cannot save the statements for future use and we
have to retype the statements to run them again.
(B) Script Mode In the script mode, we can write a Python program in a file, save it and then
use the interpreter to execute it. Python scripts are saved as files where file name has
extension “.py”. By default, the Python scripts are saved in the Python installation folder. To
execute a script, we can either:
a) Type the file name along with the path at the prompt. For example, if the name of the file
is prog5-1.py, we type prog5-1.py. We can otherwise open the program directly from IDLE
as shown in Figure 3.
b) While working in the script mode, after saving the file, click [Run]->[Run Module] from
the menu as shown in Figure 4
c) The output appears on shell as shown in Figure 5

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 2


PYTHON KEYWORDS
Every language contains words and a set of rules that would make a sentence meaningful.
Similarly, in Python programming language, there are a set of predefined words, called

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 3


Keywords which along with Identifiers will form meaningful sentences when used together.
Python keywords cannot be used as the names of variables, functions, and classes.
Python contains thirty-five keywords in its most recent version. Below is a complete list of
Python keywords for your reference:

IDENTIFIERS
In programming languages, identifiers are names used to identify a variable, function, or
other entities in a program.

These are the rules for naming identifier in Python is as follows:


1. The name should begin with an uppercase ( A to Z) or a lowercase alphabet (a-z) or
an underscore sign (_) or digits (0-9).
2. It can be of any length. (However, it is preferred to keep it short and meaningful).
3. Keyword or reserved words cannot be used as a identifier.
4. We cannot use special symbols like !, @, #, $, %, etc., in identifiers
5. Commas or blank spaces are not allowed in the identifiers.
Examples: area, radius, marks, total, average, a,b etc.,
area=length*breadth

VARIABLES:
In a program, a variable is uniquely identified by its name, known as an identifier. In Python,
a variable refers to an object, which is an item or element stored in memory. The value of a
variable can be a string, a number, or any combination of alphanumeric characters.
We can create new variables and assign specific values to them using assignment statements
in Python.
gender = 'M'
message = "Have A Nice Day
regno = 111
Program : Write a program to display values of variables in Python.

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 4


#Program 5-2

#To display values of variables


message = "Have A Nice Day"
print(message)
regno = 111
print('Register Number is',regNo)
Output:
Have A Nice Day
'Register Number is' 111
Program: Write a Python program to find the area of a rectangle given that its length is
10 units and breadth is 20 units.
#Program
#To find the area of a rectangle
length = 10
breadth = 20
area = length * breadth
print(area)
Output:
200

COMMENTS
Comments are used to add remarks or notes in the source code. They are not executed by the
interpreter.
Comments are added to make the source code easier for humans to understand. They
primarily document the meaning and purpose of the code, including its input and output
requirements. This helps us remember how the code functions and how to use it later.
In large and complex software projects, multiple programmers often work in teams, and a
program written by one programmer may need to be used or maintained by another.
In such situations, comments are essential for understanding the workings of the program.
In Python, a comment starts with a # (hash sign). Everything following the # until the end of
that line is treated as a comment, and the interpreter ignores it while executing the code.
Example
#Variable amount is the total spending on
#Books
amount = 1500

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 5


#totalMarks is sum of marks in all the tests
totalMarks = test1 + test2 + test3
Program 5-4 Write a Python program to find the sum of two numbers.
#Program 5
#To find the sum of two numbers
num1 = 10
num2 = 40
total = num1 + num2
print(total)
Output: 50
30

Everything is an Object
In Python, every value or data item, whether numeric, string, or another type (discussed in the
next section), is treated as an object. This means it can be assigned to a variable or passed to a
function as an argument. Each object in Python is given a unique identity (ID) that remains
constant for the object's lifetime. This ID is similar to the memory address of the object. The
id() function returns the identity of an object.
Example 5.2
>>> num1 = 30
>>> id(num1)
1433920576 #identity of num1
>>> num2 = 50 - 10
>>> id(num2)
1433920576 #identity of num2 and num1
#are same as both
refers to #object 40

MOHAN C ,HOD DEPT. OF COMPUTER SCIENCE Page 6

You might also like