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

1-42 getting started with python online

The document provides an overview of Python, including its uses by various organizations, its development history, and its advantages and disadvantages. It explains the concepts of programs, programming languages, source code, and the Python interpreter, along with practical examples of Python programming. Additionally, it covers Python identifiers, literals, and the rules for naming identifiers, along with examples of valid and invalid identifiers.

Uploaded by

Ishaan Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

1-42 getting started with python online

The document provides an overview of Python, including its uses by various organizations, its development history, and its advantages and disadvantages. It explains the concepts of programs, programming languages, source code, and the Python interpreter, along with practical examples of Python programming. Additionally, it covers Python identifiers, literals, and the rules for naming identifiers, along with examples of valid and invalid identifiers.

Uploaded by

Ishaan Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Getting started with

Python
Who uses Python
• Red Hat :uses python for configuring and managing Linux operating system
• Infoseek: uses Python within certain parts of its public search engines
• NASA: takes advantage of strong numerical abilities of Python and for
plotting the paths taken by satellites.
• Industrial Light and Magic (ilm.com): famous for doing the special effects
on films such as Star Wars, Star Trek and Indiana Jones uses Python to
produce commercial grade animation. In fact, if you visit their website, you’ll
see they have a number of vacancies for Python Programmers.
Points to remember
• Python Programming language was developed by Guido Van Rossum in
February 1991 at National research institute for mathematics, Netherlands.
• Python is presently owned by PSF(Python Software Foundation).
• It is influenced by two programming languages:
• ABC language which was replacement of BASIC
• Modula-3
• It is Object Oriented Programming language
Points to remember
• It is high level programming language, which means that it separates the
user from the underlying operating system as much as possible. However,
unlike other languages, python provides you with the ability to access the
operating system at a lower level if you desire.
• Python was named after famous BBC comedy show Monty Python’s Flying
Circus.
• Python is a case sensitive language.
Advantages of Python
• It is easy to learn and use as it is programmer friendly.
• Expressive language: Compact and easier to understand.
• Python follows “batteries included” philosophy because of its standard libraries and
packages for diverse functionality such as e-mail, web pages, databases, GUI development,
network connections, scientific and non scientific applications and many more.
• Portable language: it can run on variety of platforms(windows /linux /unix /mac
/supercomputers / smart phones). Python is supplied with a module to the toolkit Tk
interface libraries and it’s possible to write an application on one platform and use it on
other platforms without making any modifications.
Advantages of Python
• Interpreted language: Python interpreter interprets and executes the code line by line and
hence is easy to debug language. Although python is considered as an interpreted language,
it employs a compilation stage that translates the raw text python script into a series of
bytecodes, which are then executed by the Python Virtual Machine(PVM). The use of the
compilation and bytecode stages helps to improve performance and makes python much
faster than pure interpreters such as BASIC, but slower than the truly compiled languages
such as C and PASCAL. However, unlike many other languages, the bytecode versions of
modules can be saved and executed without having to recompile them each time they are
required, thereby improving performance by eliminating the compilation stage. Note that
the bytecode that is created is completely platform and operating system independent.
Advantages of Python
• Free and open source: it is freely available and it can be improved or extended. It means that
you can write python programs without having to purchase any software and without having
to worry about licensing issues. You can even download the source code to the python
software if you want to take a closer look at how the python language works.
• Python supports an extension called NumPy, which provides interfaces to many standard
mathematics libraries. The python language also supports unlimited precision. If you want to
add two 100 digit numbers together, you can do so with Python without requiring a third
party extension. If you need real speed in mathematical applications, the NumPy extension
written in C, operates faster than the native math supported by Python.
Advantages of Python
• Supports text processing: Python can split, separate, summarize and report on any data.
• Supports RAD (Rapid Application Development). Python is so straightforward to develop
applications with. The extensive module library that comes with Python provides direct
interfaces to many of the protocols, tools, and libraries that you would otherwise have to
develop yourself.
• Python also supports XML and HTML so that you can parse user input and produce top
quality formatted output via a web server.
Disadvantages of Python
• Not the fastest language: though python offers faster development but execution
times are not that fast as that of compiled languages.
• Lesser libraries than C, Java, Pearl
• Not strong on type binding. Shows type mismatch issues.
• Program written in python is not easily convertible to other language.
PROGRAM AND PROGRAMMING LANGUAGE

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

specific task is called a program

PROGRAMMING LANGUAGE: The language used to specify this set of instructions to

the computer is called a programming language.

SOURCE CODE: A program written in a high-level language is called source code


PROGRAM AND PROGRAMMING LANGUAGE

SOURCE CODE: A program written in a high-level language is called source code


• 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.
Download python from
• www.python.org (installation includes Cpython interpreter written in c and python,
python IDLE(Integrated development learning environment), and pip(package
installer))
• Alternatively, you can use Anaconda Python distribution which comes preloaded
with many packages and libraries. Jupyter notebook and Spyder can be used to run
python programs.
Working in CPython distribution

Interactive mode Script mode


(Python shell)
(used for testing purpose) (used for saving code in files)
Working in CPython distribution
Use python IDLE, to work in
• Interactive mode (also called python shell)
o Shows python version
o here you type one command at a time and python interpreter executes the
command and gives you output/error (if any).
o Commands are typed in front of python command prompt >>>
o You can press Alt+P to repeat a command in IDLE in order to save time and
typing effort.
o This mode is very useful for testing code
o Does not save the commands entered by you in the form of a program and
hence the output is sandwiched between the command lines.
o The solution is script mode.
Working in CPython distribution
Script mode
• Create a new file in the IDLE. Type the commands and save the program with
the extension .py
• Run the program/module/script file by
o Run →Run Module command
o Or, Press F5 key on the keyboard.
• Output/error (if any) will be shown in python shell.
Exiting python: At command prompt(>>>), type
• quit(), or
• exit()
INTERACTIVE MODE
PYTHON SHELL
SCRIPT MODE
INTERACTIVE/SCRIPT MODE

Comment entry
• Preceded with #
Program 5-2 Write a program to display values of
• Not executed by
interpreter variables in Python.
#Program 5-2
• Single line • Variable
#To display values of variables
comment entry • Identify the other variable
message = "Keep Smiling"
• Wherever a variable name
print(message)
occurs in an expression,
userNo = 101
the interpreter replaces it
print('User Number is', userNo)
with the value of that
Output: particular variable.
• print() function is Keep Smiling
to display on to User Number is 101
monitor
• Anything which
appears in single
quotes ‘ ’ or double • Note the space
quotes “ ” appears appeared in output by
as a string itself
(sequence of
characters)
INTERACTIVE/SCRIPT MODE

Write a Python program to find the area of a


rectangle given that its length is 10 units and breadth
is 20 units.
[HINT}
• Comment entry stating the aim of
the program
• Variable assignment 1
• Variable assignment 2
• Calculate area
• Display area
INTERACTIVE/SCRIPT MODE

Write a Python program to find the area of a


rectangle given that its length is 10 units and breadth
is 20 units.

#To find the area of a rectangle


length = 10
breadth = 20
area = length * breadth
print(area)
Output:
200
INTERACTIVE/SCRIPT MODE

Write a Python program to find the sum of two


numbers.
INTERACTIVE/SCRIPT MODE

Write a Python program to find the sum of two


numbers.

#To find the sum of two numbers


num1 = 10
num2 = 20
result = num1 + num2
print(result)
Output:
30
PYTHON CHARACTER SET
PYTHON TOKENS
(The smallest individual unit in a program)

TOKENS

IDENTIFIERS
KEYWORDS LITERALS OPERATORS PUNCTUATORS
(NAMES)
INTERACTIVE/SCRIPT MODE

Write a Python program to find the sum of two


numbers.

#To find the sum of two numbers


num1 = 10
num2 = 20 LITERAL

result = num1 + num2 IDENTIFIER/NAME

print(result)
OPERATOR
Output:
30 PUNCTUATOR
PYTHON KEYWORDS

• Keywords are reserved words.


• Each keyword has a specific meaning to the Python interpreter.
• 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.
PYTHON IDENTIFIERS
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 (_).
• 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 .
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
PYTHON IDENTIFIERS
identifiers are names used to identify a variable, function, or other entities in a program.

1. Name the identifiers for storing marks of three subjects.

2. Name the identifier for storing the average of the marks obtained earlier.

3. Name the identifiers required to find area of a rectangle

4. Name the identifiers required to find volume of a cuboid.


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

1. Name the identifiers for storing marks of three subjects.


marks1, marks2, marks3
2. Name the identifier for storing the average of the marks obtained earlier.
avg or average
3. Name the identifiers required to find area of a rectangle
length, breadth, area
4. Name the identifiers required to find volume of a cuboid.
length, breadth, height, volume
PYTHON IDENTIFIERS
identifiers are names used to identify a variable, function, or other entities in a program.
PYTHON IDENTIFIERS
identifiers are names used to identify a variable, function, or other entities in a program.

TASK: Which of the following identifier names are invalid and why?
i) Serial_no.
ii) 1st_Room
iii) _Percentage
iv) Total_Marks
v) Total Marks
vi) Hundred$
vii) total-Marks
viii) True
PYTHON IDENTIFIERS
identifiers are names used to identify a variable, function, or other entities in a program.

TASK: Which of the following identifier names are invalid and why?
i) Serial_no. [invalid (.)]
ii) 1st_Room [can’t begin with digit]
iii) _Percentage [valid]
iv) Total_Marks [valid]
v) Total Marks [can’t have space]
vi) Hundred$ [can’t have any special character other than _]
vii) total-Marks [can’t have any special character other than _]
viii) True [invalid as it’s a keyword]
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

LITERALS

STRING NUMERIC BOOLEAN SPECIAL


LITERAL LITERAL LITERAL LITERAL None
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

STRING LITERAL: It is a sequence of characters surrounded by quotes (single or double or triple quotes)

EXAMPLES:
‘A’ ‘a’
“a” “abc”
‘abc’ “amy’s”
‘amy”s’ ‘1-x-0-w-o-25’
“112fbD29” “123”
“!@#$%^”
‘123’
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

STRING LITERAL: It is a sequence of characters surrounded by quotes (single or double or triple quotes)

NON GRAPHIC CHARACTERS


• Those characters that can’t be typed directly from the keyboard, e.g backspace, tab, carriage return
• It means no character is typed when these keys are pressed and only some action takes place.
• These characters are represented using escape sequence backslash(\) followed by one or more characters.

LIST OF ESCAPE SEQUENCES

ESCAPE SEQUENCE WHAT IT DOES ESCAPE SEQUENCE WHAT IT DOES


\\ Backslash(\) Single quote \’
\” Double quote(”) \a Audible bell
\b Backspace \n Newline
\r Carriage return \t Horizontal tab
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

STRING LITERAL: It is a sequence of characters surrounded by quotes (single or double or triple quotes)

NON GRAPHIC CHARACTERS


ESCAPE SEQUENCE WHAT IT DOES ESCAPE SEQUENCE WHAT IT DOES
\\ Backslash(\) Single quote \’
\” Double quote(”) \a Audible bell
\b Backspace \n Newline
\r Carriage return \t Horizontal tab
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

STRING LITERAL: It is a sequence of characters surrounded by quotes (single or double or triple quotes)

STRING TYPES

SINGLE LINE STRINGS


MULTI LINE STRINGS
(BASIC STRINGS)

ADDING \(BACKSLASH)
AT THE END OF SINGLE
LINE STRING

USING TRIPLE QUOTES


PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

STRING LITERAL: It is a sequence of characters surrounded by quotes (single or double or triple quotes)

SINGLE LINE STRINGS: The strings that you create by enclosing text in single quotes or double quotes. In other words,
they must terminate in one line
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value
MULTI LINE STRINGS: These strings allow you to store text spread across multiple lines as one single string

1. By adding a backslash(\) in 2. By typing text in triple quotation


single line string mark
SHELL SCRIPT
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value
SIZE OF STRINGS

Enter pressed(called
EOL character) is
also counted here

EOL character is
NOT counted here
PYTHON LITERALS/VALUES (CONSTANT)
Data items that have a fixed value

NUMERIC
LITERALS

int float complex


• Positive or negative whole numbers • Real numbers and are • are of the form a+bj,
with no decimal point written with decimal point where a and b are floats
• (called signed integers) • Commas not allowed j/J represents −1,
• Commas can’t appear in integer • VALID (2.0, -13.0, .3, 7.) where a is real and b is
constant • INVALID(7, +17/2 , imaginary part
• Types: decimal (4,-4), octal(0o23), 17.250.65 • Example: 2+3J
hexadecimal(0x2A3F)

You might also like