0% found this document useful (0 votes)
12 views4 pages

Python Programming Basics Overview

The document provides an overview of Python programming basics, including its history, structure, and key concepts such as variables, data types, and tokens. It explains the use of Python IDLE for coding in both interactive and script modes, along with examples of Python syntax and functionality. Additionally, it covers the classification of tokens in Python, including identifiers, keywords, literals, operators, and delimiters.
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)
12 views4 pages

Python Programming Basics Overview

The document provides an overview of Python programming basics, including its history, structure, and key concepts such as variables, data types, and tokens. It explains the use of Python IDLE for coding in both interactive and script modes, along with examples of Python syntax and functionality. Additionally, it covers the classification of tokens in Python, including identifiers, keywords, literals, operators, and delimiters.
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

Review of

()
Python Basics
1.1 INTRODUCTION
You are aware of
the fact that
language. You have learnt the
Python is a powerful,
modern and the most popular programming
briefly recapitulate the basics of Python
Python concepts that you programming in Class XI. In this chapter, we will
Python is a high-level, have already learnt.
was developed by Guidoobject-oriented, interactive,
van Rossum in 1991]It general-purpose programming language which
Machine Learning and mobile is used in web development, Artificial Intelligence,
Python is an interpreted languageapplication development.
as
interpreter should be installed on the its programs are executed by an interpreterThus, ryu
have also learnt that Python IDLE computer system to write and run Python ProB
two working (Integrated Develonment and Learning
main window modes-Interactive mode (popularly known as Python Shell)Environmeny provie
types-the Shell window and the Editor and Script moue e
window.
CIM: The Python IDLE tool offers an interactive and a
more effcient platform to write your code in Pytnon.
Using Interactive (Python Shell window) mode, the
(command prompt, three greater than symbols appearing Python commands are directly typed at >>>
press the Enter key, the interpreter displays the result(s) adjacent to each other) and as soon as We
immediately, which is known as Displaying
For example,
>>> print ("Welcome to Python
Welcome to Python Programming Programming")
>>> print (10+50)
60
>>> 10
>>> y50
>> print (z * y)
500
>>>
Fig. 1.1: Working with Python Shell

Script Mode
(The drawback of interactive mode is that we cannot save the commands that we type. This
can be
overcome using the script mode. In order to switch over to script mode, click on the File menu
option ’ New option or press the shortcut key Ctrl +Nfrom the shell window.
For example,
observe the
following script
category too. and enlist
#Identify tokens in
X=68
all the
tokens uSed in it. Por each token, write its
the script.
After y=12
sode)
ts. z=X/y
print ("x Y z
Token
are:", X,y, z)
y
Category ldentifier/ Varlable Print 68 12 "%, Y, z are:"
Variable Variable Method Literal Literal Literal
Delimiter/
Operator
Operator

1.3 STRUCTURE
OF A PYTHON
A Python
program PR0GRAM
comments, etc., whichconstitutes several expressions, functions,

are synchronized elements such as statements,


e in the manner as
shown beoe
programs consist of modules Contain statements
contain

expressions
create and process

objects

Let us see the several components of a basic


Python program.
Python Progam
# This program describes the struature of a
# Definition of function greet () follows
Comments
(Start with #) def greet () :
print ("Hello Python Learners") Function

#Main Program
a 1O
Expressions
Statements 204
a b + a
print (a)
print (b + 50) Blocks
print (o)

age 15 it's a block


|if age > #colon in front of£ if signifie
18:
print ("Eligible to vote")
Indentation else:
+print ("Not Eligible to
vote")

greet ()
greet() #Calling the above defined function

Inline Comments
Function call line)
(Comments beginning in the middle of a

-Review of Python Basics


As shown in thesnippet given above, the
Expressions: An expression several components that a
value is an expression. represents something, like a Python program holds are:
number, a: string, or an element. Any
Statements: Anything that does something is a
function call is a statement. Any value contained instatement. Any assignment to a variable or
Comments: Comments are additional that statement 1s an
information expPesso
of code for better clarity of provided against a statement or a chunk
the code, Interpreter ignores them
and
in commands. Symbols used for
writing comments include Hash (#) does or
not count them
Triple
Triple Double (") quotation marks. Hash (#) is Single (")/
do not span multiple lines. Triple quotation marks used for writing single-line comments that
(" or ""*) are used to write multiple-line
comments; three triple quotation marks to start the comment and
to end the comment.) again three quotation marks
Functions: Function is a set of instructions defined under a particular name that
performs a
specific task which, once written, can be called whenever and wherever required.
Block(s): A block refers to a group of statements which are part of another statement or
function. All statements inside a block are indented at the same level.

1.4 VARIABLES AND DATA TYPES


variable is like a container that stores values you can access or change in a program. The purpose
A
of using variables is to allow the stored values to be used later. We have learnt that any object
or
variable in Python is a name that refers to a value at a particular memory location and possesses
three components:
> Value: It represents any number or a letter or a string. To assign any value to a variable, we use
assignment operator (-).
> Identity: It refers to the address of the variable in memory which does not
change once created.
To retrieve the address (identity) of a variable, the command used is:
>>> id (variable name)
Type: We are not required to explicitly declare a variable with its type. Whenever we declare
a variable with some value, Python automatically allocates the relevant data
with it.
type associated

Hence, the data type of a variable is according to the value it holds.


For example, >>> x = 20
The above statement signifies 'x' to be of integer
type since has been assigned an integer
value 20.
To determine the data type of a variable, the command
used is:
>>>type (variable name or data)
>>>type (x)
<class 'int'>

Computer Science with Python-XIl -


Example 1:

For
Output:
print ("x La:" ia: 4 Ob:
print ("y ie:", y) Y is: 5 cat
|print("z is:", )

Exampe 2: Lokesh has taken a loan of 40000 from Vinod at an interest rate of 8%
6 years, he wants to repay the loan in full including interest. Write the Python code per annum. After
to calculate and display the interest and total amount to be paid by Lokesh to settle(in script mode
his
Prinoipal
Rate 8
40000
accounts.
Time = 6
simple Int Prinoipal * Rate * Time/ 10o
Amount - Prinoipal + 31mple Int
Iprint("Simple Interest Rs.", Simple Int) Output:
print ("Amount Payable = Rs., Amount) Simple Interest Rs. 19200.0
|Amount Payable RS. 59200.0
DÍfferent types of Files in Python
By default, a file is saved with .py extension
in Python. However, there are
extensions which Python supports for specific applications different types of file
.py: The normal extension for a Python source
listed below:
file
.pyc: The compiled bytecode
.pyd: A Windows DLL file for Python
" pyo:A file created with optimizations
pyw: A Python script for Windows
pyz: A Python script archive

L2TOKENS
, Atoken is the smallest element of a
are five categories of tokens which arePython program that is meaningful to the
as under: interpreter. There
. Jdentifiers: The name of any
variable, constant, function or module is called
identifier is made with a combination of lowercase (a-z) or uppercase an identifier. The Python
underscore (). Examples of identifiers are: abc, (A-Z) letters, digits (0-9) or an .
x12y, india_123, swap, stud, Roll_no,
Keywords: The reserved words of Python, which etc.
are called keywords. No keyword can be have a special fixed meaning for the interpreter,
used as an identifier.
iterals: A fixed numeric or non-numeric
value is called a literal. It can be
text or other data that represents defined as a number,
2, -23.6, "abc", 'Independence, values to be stored in variables. Examples of literals are:
etc.
Wperators: An operator is a symbol or a word that
values and returns the result. Examples performs some kind of operation on given
of operators are: +, -, **, /, etc.
Delimiters: Delimiters are symbols which can be
used as separators of values or to
values. Examples of delimiters are: 0 enclose sonie
{OO;:
Note: # symbol used to insert comments is
not a token. Any comment itself is not
a token.
12
-Computer Science with Python-XIl

You might also like