0% found this document useful (0 votes)
6 views

class_11cs_unit2_Computational_Thinking_and_Programming_–_1_Part (4)

This document provides an introduction to Python programming, covering its features, execution modes, character set, tokens, identifiers, literals, operators, and variables. It explains the basics of writing and executing Python programs, including the use of comments and the distinction between l-value and r-value. The content is aimed at students in Class XI Computer Science, focusing on foundational concepts in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

class_11cs_unit2_Computational_Thinking_and_Programming_–_1_Part (4)

This document provides an introduction to Python programming, covering its features, execution modes, character set, tokens, identifiers, literals, operators, and variables. It explains the basics of writing and executing Python programs, including the use of comments and the distinction between l-value and r-value. The content is aimed at students in Class XI Computer Science, focusing on foundational concepts in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit II

Computational Thinking and


Programming – 1
(PART -2 )

CLASS XI COMPUTER SCIENCE


CODE 083
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

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
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

Introduction to Python
What is Python?
• Python is a popular programming language. It was created by Guido
van Rossum and released in 1991.
• It is used for web development (server-side),software development,
mathematics, system scripting.

What is a programming language ?


• An ordered set of instructions to be executed by a computer to carry
out a specific task is called a program . The language used to specify
this set of instructions to the computer is called a programming
language.
Features of Python SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Python is a high level language. It is a free and open source language.


• It is an interpreted language, as Python programs are executed by an
interpreter.
• Python programs are easy to understand as they have a clearly defined
syntax and relatively simple structure.
• Python is case-sensitive. For example, NUMBER and number are not same
in Python.
• Python is portable and platform independent, means it can run on various
operating systems and hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web services and
applications are built using Python.
• Python uses indentation for blocks and nested blocks.
Python Getting Started SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

To write and run (execute) a Python program, we need to have a Python


interpreter installed on our computer or we can use any online Python
interpreter. The interpreter is also called Python shell.

Python Install
You can download it for free from
the following
website: https://www.python.org/
A sample screen of Python interpreter is
shown the symbol >>> is the Python
prompt, which indicates that the
interpreter is ready to take instructions
Execution Modes SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

There are two ways to use the Python interpreter:

1. 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)
• Working in the interactive mode is convenient for testing a single
line code for instant execution.
• In the interactive mode, we cannot save the statements for future
use.
• we have to retype the statements to run them again.
Execution Modes SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

2. Script mode
• In the script mode, we 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 helloworld.py, we type helloworld.py
• We can otherwise open the program directly from IDLE as While working in
the script mode, after saving the file, click [Run]->[Run Module] from the
menu
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

Executing a simple "hello world" program


• Interactive mode Script Mode
Python Character Set
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

Character set is the set of valid characters that a language can


recognize.
A character represents any letter, digit or any other symbol.

• Python has the following character sets: ·


• Letters – A to Z, a to z ·
• Digits – 0 to 9 ·
• Special Symbols - + - * / etc. ·
• Whitespaces – Blank Space, tab, carriage return, newline,
formfeed ·
• Other characters – Python can process all ASCII and Unicode
characters as part of data or literals.
Python tokens
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

In a passage of text, individual words and


punctuation marks are called tokens lexical unite or
lexical elements.
The smallest individual unit in a program is called
token.
Python has the following tokens:
(i) Keyword (ii) Identifiers (iii) Literals (iv)
Operators (v) Punctuators
Python Keywords
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Keywords are some predefined and reserved words in python that


have special meanings. Keywords are used to define the syntax of the
coding.
• The keyword cannot be used as an identifier, function, and variable
name.
• All the keywords in python are written in lower case except True and
False.
• Ex -
Python Identifiers
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• In programming languages, identifiers are names used to


identify a variable, function, or other entities in a program
• It consists of letters and digits in any order except that the
first character must be a letter, the underscore (_) counts
as a character.
• Identifiers are unlimited in length.
• Python is a case sensitive programming language. Thus
„Value‟ and „value‟ are two different identifiers in Python
Rules for Identifier
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• It consist of letters and digits in any order except that the


first character must be a letter.
• The underscore (_) counts as a character.
• Spaces are not allowed.
• Special Characters are not allowed. Such as @,$ etc.
• An identifier must not be a keyword of Python.
• Variable names should be meaningful which easily depicts
the logic.
• Example - add, add123,ADD123,Add_no,_abc,
Literals
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

Literals (constants) are data items that have a fixed value.


Python allows several kinds of literals:
• String literals : Text enclosed in quotes form a string literals in Python.
E.g. „a‟, „abc‟, “abc”.
• Numeric literals : Numerical literals in Python are those literals that
contain digits only . Ex Integer (1,2,3), Float(2.45 ),Complex(2+3i)
• Boolean literals –Boolean literals in Python are pretty straight-forward
and have only two values- True- (1). False-(0).
• Special Literal None - Python literals have one special literal known
as None. This literal in Python is used to signify that a particular field is
not created.
• Literals Collections - If we wish to work with more than one value, then
we can go for literal collections in Python. Literal collections in Python
are of four types List , tuple dictionary, set.
Operator
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Operators in Python are special symbols that carry


arithmetic or logical operations.
• The value that the operator operates on is called the
operand.
• In Python, there are seven different types of operators:
arithmetic operators, assignment operators, comparison
operators, logical operators, identity operators,membership
operators, and Boolean operators.
Punctuators
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Punctuators are nothing but symbols used in programming


languages to organize sentence structures, and indicate the
rhythm and emphasis of expressions, statements and
program structure.

• Examples of punctuators in python: „, “, #, \, /, (, ), [,


], {, }, @, ;, :, . Etc
Variable
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• A variable in Python is a reserved memory location to store values. In


other words, a variable in a python program gives data to the computer
for processing. Declaring and re-declaring a variable in Python is as
easy as writing names and assigning values to it.
Creating a variable:
• A variable is created the moment you first assign a value to it.
Example: x = 5 y = “hello”
• Variables do not need to be declared with any particular type and can
even change type after they have been set. It is known as dynamic
Typing.
x=4 # x is of type int
x = "python" # x is now of type str
print(x)
Rules for Python variables
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• A variable name must start with a letter or the


underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscore (A-z, 0- 9, and _ )
• Variable names are case-sensitive (age, Age and
AGE are three different variables)
Lvalue and Rvalue:
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• An expression has two values. Lvalue and Rvalue.


Lvalue: the LHS part of the expression
• Rvalue: the RHS part of the expression
• Python first evaluates the RHS expression and then
assigns to LHS.
• Example:
p, q, r= 5, 10, 7
q, r, p = p+1, q+2, r-1
print (p,q,r)
Now the result will be: 6 6 12
Use of comments
SUBSCRIBE HAMARI ACADEMY ON YOUTUBE

• Comments are not executed.


• Comments explain a program and make a program understandable and
readable.
• All characters after the # and up to the end of the physical line are part of the
comment and the Python interpreter ignores them.

There are two types of comments in python:


• Single line comment: This type of comments start in a line and when a line
ends, it is automatically ends. Single line comment starts with #
Example: if a>b: # Relational operator compare two values

• Multi-Line comment: Multiline comments can be written in more than one


lines. Triple quoted „ ‟ ‟ or “ ” ”) multi-line comments may be used in python. It
is also known as docstring.
THANKS FOR WATCHING..
DON’T FORGET TO SUBSCRIBE
SHARE WITH FRIENDS ALSO

You might also like