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

Programming With Python CHAPTER 01 (1)

The document provides an introduction to Python programming, covering its syntax, features, and execution process, including the conversion of source code into bytecode and its execution by the Python Virtual Machine. It discusses Python identifiers, keywords, indentation, variables, comments, and various data types such as numbers, strings, lists, tuples, and dictionaries. Additionally, it includes programming exercises for practical application of the concepts learned.

Uploaded by

shivamteli07
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)
5 views

Programming With Python CHAPTER 01 (1)

The document provides an introduction to Python programming, covering its syntax, features, and execution process, including the conversion of source code into bytecode and its execution by the Python Virtual Machine. It discusses Python identifiers, keywords, indentation, variables, comments, and various data types such as numbers, strings, lists, tuples, and dictionaries. Additionally, it includes programming exercises for practical application of the concepts learned.

Uploaded by

shivamteli07
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
You are on page 1/ 43

PROGRAMMING WITH PYTHON

RESOURCE PERSON: Ms. SANGEETAWANKHADE


UNIT 1 INTRODUCTION AND SYNTAX OF PYTHON PROGRAM
(8 Marks)
Identify the given variables, keywords and
constants in python.

Use indentation, comments in the given program.

COURSE OUTCOMES
Install the given python IDE and editor.

Develop the python program to display the given


text.
► Simple to learn and use
► Open Source
Features of
► High Level Language
Python ► Dynamically Typed
► Platform Independent
► Portable
► Procedure and Object Oriented
Execution of
Python
Program
What is Bytecode?

Bytecode is an intermediate representation of your source code. It's a low-level set


of instructions that is platform-independent, meaning it can run on any operating
system with a compatible Python interpreter.

Here's a simplified view of the process:

Source Code (.py): The original Python script.


Bytecode (.pyc): Compiled version of the script, optimised for execution.
Python Virtual Machine (PVM): Executes the bytecode.
This process ensures that Python code is portable and can be executed efficiently
on any platform.
The Compilation Process

When you run a Python script, Python automatically compiles it into bytecode. This bytecode is stored in .pyc files
in a __pycache__ directory.

Here's a step-by-step breakdown:

Write the Code: Create a Python script (hello.py).


Run the Script: Python compiles the script to bytecode.
Execute the Bytecode: The PVM executes the bytecode.

Python Virtual Machine

Python Virtual Machine (PVM) is a program which provides programming environment. The role of PVM is to
convert the byte code instructions into machine code so the computer can execute those machine code instructions
and display the output.
Interpreter converts the byte code into machine code and sends that machine code to the computer processor for
execution.
Internal Working of Python
How is Python Source Code Converted into Executable Code
The Python source code goes through the following to generate an executable code
•Step 1: The Python compiler reads a Python source code or instruction in the code editor. In this first stage, the execution of
the code starts.
•Step 2: After writing Python code it is then saved as a .py file in our system. In this, there are instructions written by a Python
script for the system.
•Step 3: In this the compilation stage comes in which source code is converted into a byte code. Python compiler also checks
the syntax error in this step and generates a .pyc file.
•Step 4: Byte code that is .pyc file is then sent to the Python Virtual Machine(PVM) which is the Python interpreter. PVM
converts the Python byte code into machine-executable code and in this interpreter reads and executes the given file line by
line. If an error occurs during this interpretation then the conversion is halted with an error message.
•Step 5: Within the PVM the bytecode is converted into machine code that is the binary language consisting of 0’s and 1’s.
This binary language is only understandable by the CPU of the system as it is highly optimized for the machine code.
•Step 6: In the last step, the final execution occurs where the CPU executes the machine code and the final desired output will
come as according to your program.
Installation of Python
Installation of Anaconda
Learn to Use Google Colab
PYTHON BUILDING BLOCKS

IDENTIFIERS KEYWORDS INDENTATION VARIABLES COMMENTS


Python Identifiers are user-defined names to represent
a variable, function, class, module or any other
object. If you assign some name to a programmable
entity in Python, then it is nothing but technically
called an identifier.
IDENTIFIERS

Python language lays down a set of rules for


programmers to create meaningful identifiers.
RULES FOR WRITING IDENTIFIERS

• You can’t use reserved keywords as an identifier name.


• Python identifier can contain letters in a small case (a-z), upper case (A-Z), digits (0-9),
and underscore (_).
• Identifier name can’t begin with a digit.
• Python identifier can’t contain only digits.
• Python identifier name can start with an underscore.
• There is no limit on the length of the identifier name.
• Python identifier names are case sensitive which means num and NUM are two
different variables in python.
EXAMPLES
◾ Python Valid Identifiers Example
◾ Python Invalid Identifiers Example
• ab10c: contains only letters and
• 99: identifier can’t be only digits
numbers
• 9abc: identifier can’t start with
• abc_DE: contains all the valid number
characters • x+y: the only special character
allowed is an underscore
• _: surprisingly but Yes, underscore
is a valid identifier • for: it’s a reserved keyword

• _abc: identifier can start with an


underscore
KEYWORD
◾ Keywords are special words
which are reserved and have a
specific meaning.
◾ Python has a set of keywords that
cannot be used as variables in
programs.
◾ All keywords in Python are case
sensitive.
◾ So, you must be careful while
using them in your code. We’ve
just captured here a snapshot of
the possible Python keywords.
◾ There are total 33 keywords in
Python 3.6.
INDENTATION
◾ Indentation in Python is
used to create a group of
statements.

◾ Many popular languages


such as C, and Java uses
braces ({ }) to define a
block of code, Python use
indentation.
When writing python code, we have to define a group of
statements for functions and loops. This is done by
properly indenting the statements for that block.
The leading whitespaces (space and tabs) at the start of a
line is used to determine the indentation level of the line.
You have to increase the indent level to group the
statements for that code block. Similarly, reduce the
indentation to close the grouping.

HOW PYTHON INDENTATION WORKS?


A COMPARISON OF C & PYTHON WILL HELP YOU
UNDERSTAND IT BETTER.
ADVANTAGES AND DISADVANTAGES

◾ Benefits of Indentation in Python ◾ Disadvantages of Indentation in Python

• In most of the programming language, indentation • Since whitespaces are used for indentation, if the code
is used to properly structure the code. In Python, is large and indentation is corrupted then it’s really

it’s used for grouping, making the code tedious to fix it. It happens mostly in copying the code
from online sources, Word document, or PDF files.
automatically beautiful.
• Most of the popular programming languages use braces
• Python indentation rules are very simple. Most of
for indentation, so anybody coming from other side of
the Python IDEs automatically indent the code for
the development world finds it hard at first to adjust to
you, so it’s very easy to write the properly
the idea of using whitespaces for the indentation.
indented code.
PROGRAMMING WITH PYTHON
RESOURCE PERSON: Ms SANGEETAWANKHADE
VARIABLES
◾ A variable can be considered a storage container for data.
◾ Every variable will have a name.
◾ A Programmer should always choose a meaningful name for
their variable.
◾ Rules for Variable Names:
• A variable name can only start with a Letter or the
Underscore ( _ ) character.
• A variable name cannot start with a number or digit.
• A variable name can only contains the Alpha-Numeric
characters and Underscore character( like A-z , 0-9 and
_ ).
• The Variable names are case – sensitive ( car, Car and
CAR are three different variables ).
◾ You can’t use special characters like !, @, #, $, % etc. in
variable name.
COMMENTS

◾ Comments can be used to


explain Python code.
◾ Comments can be used to
make
the code more readable.
◾ Comments can be used to
prevent execution when testing
code.
◾ Comments are descriptions
that
help programmers better
understand the intent and
functionality of the program.
◾ They are completely ignored
by the Python interpreter.
In Python, there are two types of comments

1.Single-Line Comment 2. Multi-Line Comment


ADVANTAGES OF USING COMMENTS

◾ Using comments in programs makes our code more understandable. It makes the program
more readable which helps us remember why certain blocks of code were written.

◾ Other than that, comments can also be used to ignore some code while testing other blocks of
code. This offers a simple way to prevent the execution of some lines or write a quick
pseudo-code for the program.
PYTHON DATA
TYPES
NUMBER(INTEGER)

◾ A number is used to store numeric values.


◾ Python creates Number objects when variety is assigned to a variable.
◾ For example:
◾ a=3 ,
b = 5 # here a and b are number objects in which we pass 3 and 5
PYTHON SUPPORTS 4 TYPES OF NUMERIC DATA

◾ Int
◾ (signed integers like 13, 2, 29, etc.)
◾ Long
◾ (long integers used for a higher range of values like 90809080L, -0x192929L, etc.)
◾ Float
◾ (float is used for floating-point numbers like 1.9, 9.902, 15.2, etc.)
◾ complex
◾ (complex numbers like 2.14j, 2.0 + 2.3j, etc.)
STRING

◾ The string can be defined as the sequence of characters represented in the quotation(“ ”) marks.
◾ We will use single, double, or triple quotes to define a string in Python.
◾ Single Quote string:
◾ String handling in python is a straight forward task since there are various inbuilt functions and operators
provided.
◾ For Example:
◾ •A=‘Fireblaze’
Here A is having a single word Fireblaze so here we pass only single Quote.
• B=”Kartik’s friend”
Here B is having the ‘s after a Kartik if we use a single quote the word will phrase will end there for we use
double quote.
• C= ”’His friend’s,”laptop is good””’
Here we use a double quote inside the statement for that we have to pass a triple quote in such situation.
THE FOLLOWING EXAMPLE SHOWS THE STRING HANDLING IN
PYTHON

str1=’DataScience’
str2= ‘Python’

print(str1[0:2])

print(str1[4])

print(str1*2)

print(str1+str2)
LIST

Lists are similar to The items stored in Declaring a list is pretty


arrays in C. the list are separated straight forward. Items
However; the list can with a comma (,) and separated by commas
contain data of enclosed within areenclosed within
various types. square brackets []. brackets[].

The concatenation operator


a = [1, 2.2, 'python'] We can use slice [:] (+) and repetition operator
operators to access (*) works with the list in the
the data of the list. same way as they were
working with the strings.
EXAMPLE: ◾ list : l= [1, 2] Here we create a list of two numbers 1,2

◾ print (l)
◾ print (l + l)
A tuple is similar to the list in many ways.

Like lists, tuples also contain the gathering of the things


of various data types.

TUPLE
The items of the tuple are separated with a comma (,)
and enclosed in parentheses ().

A tuple may be a read-only arrangement as we will


not modify the dimensions and value of the things
of a tuple.
EXAMPLE OF THE TUPLE

◾ Tuple1 = ('SQL', 'Python', 100, 200);

◾ Tuple2 = (1, 2, 3, 4);

◾ print ("tuple1[0]: ", tuple1[0])

◾ print ("tuple2[1:4]: ", tuple2[1:4])


◾ Dictionary is an unordered set of a key-value pairs.

◾ It is like an associative array or a hash table


where each key stores a selected value.
DICTIONARY ◾ The items within the dictionary are separated with
the comma and enclosed within the curly braces
{}.

◾ In Python, dictionaries are defined within braces


{} with each item being a pair in the form
key:value. Key and value can be of any type.
◾ dict1 = {1: 'a', 2: 'b'}
EXAMPLE
◾ dict2 = {'c': 3, 'd': 4}
◾ dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}
Write a program to display message “welcome to
Juypter Notebook”.

Write a program to perform addition of 2 number


and display the result.
PROGRAMS
Write a program to develop calculator and
perform arithmetic operations.

Write a program to display area of circle.


1 2 3
Write a program to Write a program to Write a Python
display area of display area of program to swap two
rectangle by taking triangle by taking variables.
users input. users input.

QUESTIONS?
Thank You…!!!

You might also like