0% found this document useful (0 votes)
5 views69 pages

2) CS11 Unit Ii P2

The document serves as an introductory guide to Python programming, covering basic concepts such as execution modes, tokens, variables, and comments. It explains the differences between programs and programming languages, as well as the role of language translators like interpreters and compilers. Additionally, it provides instructions for downloading Python, using the interpreter, and writing Python code in both interactive and script modes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views69 pages

2) CS11 Unit Ii P2

The document serves as an introductory guide to Python programming, covering basic concepts such as execution modes, tokens, variables, and comments. It explains the differences between programs and programming languages, as well as the role of language translators like interpreters and compilers. Additionally, it provides instructions for downloading Python, using the interpreter, and writing Python code in both interactive and script modes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

GETTING STARTED WITH

PYTHON
Topics Covered

• Familiarization with the basics of Python programming: Introduction to


Python, features of Python, executing a simple "hello world" program,
execution modes: interactive mode and script mode, Python character
set, Python tokens (keyword, identifier, literal, operator, punctuator),
variables, concept of l-value and r-value, use of comments .
1)What is the difference
between a Program and
Programming Language?

Pre-
Assessmen 2)What do you understand by
source code?
t
3)What are language
translators?
Difference between a Program and
Programming Language?

• An ordered set of instructions to be executed by a computer to carry out a


specific task is called a program, and the language used to specify this set
of instructions to the computer is called a programming language. Eg:-
Python, Java, C++ etc.
A program written in a high-level language is called source code.

Language translators like compilers and interpreters are needed to translate the source code into
machine language. Python uses an interpreter to convert its instructions into machine language, so that
it can be understood by the computer. An interpreter processes the program statements one by one, first
translating and then executing. This process is continued until an error is encountered or the whole
program is executed successfully. In both the cases, program execution will stop.

On the contrary, a compiler translates the entire source code, as a whole, into the object code. After
scanning the whole program, it generates error messages, if any.
Learning Objective:

To familiarize with the basics of Python Programming.

To list the features of Python, understand execution modes


in Python, Python character set, Python tokens .

To define variables, concept of l-value and r-value, use of


comments etc.
Success Criteria
Learners can,
1) explain the basic concepts of python
2)apply the basics of python to execute a simple python program
3)find the output and correct error(s)
Features
of Python
Downloading Python
• The latest version of Python 3 is
available on the official website:
https://www.python.org/
All student MUST download and
install Python in their Laptop.
Working with Python
To write and run (execute) a
Python program, we need to have
a Python interpreter installed on In the above screen, the symbol >>> is the
our computer, the interpreter is Python prompt, which indicates that the
also called Python shell. A sample interpreter is ready to take instructions. We can
screen of Python interpreter is type commands or statements on this prompt
shown in Fig 5.1 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.
• 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 5.2.
Advantage and Disadvantage of 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.
Interactiv
e Mode
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:
• 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 5.3.
• b) While working in the script mode, after saving the
file, click [Run]->[Run Module] from the menu as
shown in Figure 5.4.
Tokens
The smallest individual unit in a program is known as tokens.

Python has the following tokens:


• Keywords
• Identifiers(Name)
• Literals
• Operators
• Punctuators
Python Keywords
• Keywords are reserved words. Each keyword has a specific meaning to
the Python interpreter, and we can use a keyword in our program only
for the purpose for which it has been defined. As Python is case
sensitive, keywords must be written exactly as given in Table 5.1.
Identifiers
• In programming languages, identifiers are names used to identify a
variable, function, or other entities in a program. The rules for naming an
identifier in Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an
underscore sign (_). This may be followed by any combination of characters
a–z, A–Z, 0–9 or underscore (_). Thus, an identifier cannot start with a digit.
• It can be of any length. (However, it is preferred to keep it short and
meaningful).
• It should not be a keyword or reserved word given in Table 5.1.
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
Independent Task(Challenge1)
Self Check(Independent Task)
Variables
• A variable in a program is uniquely identified by a name
(identifier). Variable in Python refers to an object — an item
or element that is stored in the memory. Value of a variable
can be a string (e.g., ‘b’, ‘Global Citizen’), numeric (e.g., 345)
or any combination of alphanumeric characters (CD67). In
Python we can use an assignment statement to create new
variables and assign specific values to them.
Example:
gender = 'M’
message = "Keep Smiling"
price = 987.9
Independent
Task
Self Check
lvalue : expressions that can come
on the lhs(left hand side) of an
assignment. They are objects to
which you can assign a value.
lvalues
and rvalue : expressions that can come
rvalues on the rhs(right hand side ) of an
assignment. They are the literals and
expressions that are assigned to
lvalues.
Activity
Comments

• Comments are used to add a remark or


a note in the source code. Comments
are not executed by interpreter.
• They are added with the purpose of
making the source code easier for
humans to understand. They are used
primarily to document the meaning and
purpose of source code and its input
and output requirements, so that we
can remember later how it functions
and how to use it.
• In Python, a comment starts with #
(hash sign). Everything following the #
till the end of that line is treated as a
comment and the interpreter simply
ignores it while executing the statement.
Independe
nt Task
Output(In
dependen
t Task)
Challenge

2)

3)

4)
Correction
---------------
num=int(input(“Enter any number”))
Correction
---------------
num=int(input(“Enter any number”))
1)Multiline strings can be created in two ways:
a)By adding a backslash at the end of normal
single-quote or double-quote strings,
Example:- Text=“Welcome \

Answer(Ju To\
Python”
st a b)By typing the text in triple quotation marks.(No
minute) backslash needed at the end of the line).
Eg:-Str1=“””Welcome
to
Python”””
• 41.678 – Floating point
• 12345 – Integer
• True – Boolean
• ‘True’ – String
Answer(Ju • “False”- String
st a • OxCAFE – Integer(Hexadecimal)
minute) • 0o456 – Integer(Octal)
• 0o971 – Invalid token(beginning with
0o ,means it is octal number but digits 8 and
9 are invalid digits in octal numbers)
Summary
Can anyone student summarize the important points?
Homework(in notebook)

What is the What are literals in What factors guide


difference between Python? How many the choice of
keyword and types if literals are identifiers in
identifier? allowed in Python? programs?

What is the
What is None literal difference between What is the use of
in Python? an expression and a Comments?
statement in Python?

You might also like