Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

Unit1 - Introduction To Python

The document discusses the features and basics of the Python programming language. It covers Python's object-oriented nature, use of indentation, being free and open source, power and portability, ease of use and learning, interpretation, and interactive capabilities. It also discusses Python building blocks like variables, expressions, statements, comments, and the environment setup process.

Uploaded by

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

Unit1 - Introduction To Python

The document discusses the features and basics of the Python programming language. It covers Python's object-oriented nature, use of indentation, being free and open source, power and portability, ease of use and learning, interpretation, and interactive capabilities. It also discusses Python building blocks like variables, expressions, statements, comments, and the environment setup process.

Uploaded by

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

SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

Chapter 1 Marks 08
Introduction & syntax of Python Programming
Introduction:
Python is an excellent language with which to learn programming. There are many reasons for this, but the
simple explanation is that it’s easy to read and fast to write; it doesn’t take long to come up with working
code that does something meaningful. Python has a very human-friendly syntax, which makes writing
elegant code easy. The basic language is fairly simple and therefore easy to remember, and then it has an
extensive library of predefined functions that you can use to facilitate most common computer tasks.
Writing effective applications in Python can be as simple as playing with conceptual building blocks. It
works really well for writing a little two-line application to perform some routine system administration
task or to provide interactive functions on a web page, but it has enough power and flexibility to
comfortably create much larger and more complex applications with graphic interfaces indistinguishable
from the programs you are used to running from your computer’s main menu.

1.1 Features of Python:


The following are the primary factors to use python in day-to-day life:

1. Python is object-oriented
Structure supports such concepts as polymorphism, operation overloading and multiple inheritances.

2. Indentation
Indentation is one of the greatest features in python

Python represents block structure and nested block structure with indentation, not with begin and end
brackets.
Benefits of the use of indentation to indicate structure:

 Reduces the need for a coding standard. Only need to specify that indentation is 4 spaces and no
hard tabs.
 Reduces inconsistency. Code from different sources follow the same indentation style. It has to.
 Reduces work. Only need to get the indentation correct, not both indentation and brackets.
 Reduces clutter. Eliminates all the curly brackets.

3. It’s free (open source)


Downloading python and installing python is free and easy

4. It’s Powerful
 Dynamic typing
 Built-in types and tools
 Library utilities
 Third party utilities (e.g. Numeric, NumPy, sciPy)
 Automatic memory management

Mr. P R Sali 1 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

5. It’s Portable
 Python runs virtually every major platform used today
 As long as you have a compatible python interpreter installed, python programs will run in exactly
the same manner, irrespective of platform.

6. It’s easy to use and learn


 No intermediate compile
 Python Programs are compiled automatically to an intermediate form called byte code, which the
interpreter then reads.
 This gives python the development speed of an interpreter without the performance loss inherent
in purely interpreted languages.
 Structure and syntax are pretty intuitive and easy to grasp.

7. Interpreted Language
Python is processed at runtime by python Interpreter

8. Interactive Programming Language


Users can interact with the python interpreter directly for writing the programs

1.2 Python building blocks:


Variables:
Variables are nothing but reserved memory locations to store values. This means that when you create a
variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory. Therefore, by assigning different data
types to variables, you can store integers, decimals or characters in these variables.
Rules for Python variables:
• 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 underscores (A-z, 0-9,
and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)

Assigning Values to Variables:


Python variables do not need explicit declaration to reserve memory space. The declaration happens
automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the right of the =
operator is the value stored in the variable.
For example −
a= 100 # An integer assignment
b = 1000.0 # A floating point
c = "John" # A string
print (a)
print (b)
print (c)

This produces the following result −


100
1000.0
John
Mr. P R Sali 2 PWP (22616)
SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

Multiple Assignments:
Python allows you to assign a single value to several variables simultaneously.
For example:
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory
location. You can also assign multiple objects to multiple variables.
For example −
a,b,c = 1,2,"SNJB“
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively,
and one string object with the value "john" is assigned to the variable c.
Output Variables:
The Python print statement is often used to output variables.
Variables do not need to be declared with any particular type and can even change type after
they have been set.

x = 5 # x is of type int
x = "SNJB " # x is now of type str
print(x)
Output: SNJB

To combine both text and a variable, Python uses the “+” character:
Example
x = "awesome"
print("Python is " + x)
Output
Python is awesome
You can also use the + character to add a variable to another variable:
Example
x = "Python is "
y = "awesome"
z=x+y
print(z)
Output:
Python is awesome

Expressions:
An expression is a combination of values, variables, and operators. An expression is evaluated using
assignment operator.
Examples: Y=x + 17
>>> x=10
>>> z=x+20
>>> z
30

>>> x=10
>>> y=20
>>> c=x+y
>>> c
30

A value all by itself is a simple expression, and so is a variable.

Mr. P R Sali 3 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

>>> y=20
>>> y
20

Python also defines expressions only contain identifiers, literals, and operators. So,
Identifiers: Any name that is used to define a class, function, variable module, or object is an identifier.
Literals: These are language-independent terms in Python and should exist independently in any
programming language. In Python, there are the string literals, byte literals, integer literals, floating point
literals, and imaginary literals.
Operators: In Python you can implement the following operations using the corresponding tokens.

Statements:
A statement is an instruction that the Python interpreter can execute. We have normally two basic
statements, the assignment statement and the print statement. Some other kinds of statements that are if
statements, while statements, and for statements generally called as control flows.
Examples:
An assignment statement creates new variables and gives them values:
>>> x=10

Comments:
Single-line comments begins with a hash(#) symbol and is useful in mentioning that the whole line should
be considered as a comment until the end of line.
A Multi line comment is useful when we need to comment on many lines. In python, triple double quote(“
“ “) and single quote(‘ ‘ ‘)are used for multi-line commenting.

1.3 Python environment setup


Before we start Python programming, we need to have an interpreter to interpret and run our programs.

Installation:
There are many interpreters available freely to run Python scripts like IDLE (Integrated Development
Environment) which is installed when you install the python software from http://python.org/downloads/

Steps to be followed and remembered:


Step 1: Select Version of Python to Install.
Step 2: Download Python Executable Installer.
Step 3: Run Executable Installer.
Step 4: Verify Python Was Installed On Windows.

Step 5: Verify Pip Was Installed.


Step 6: Add Python Path to Environment Variables (Optional)

Home page of Python will have displayed as shown in Fig.1

Mr. P R Sali 4 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

Fig. 1: Home Page

Click on Download Python 3.9.2 installation window will be open as shown in Fig 2.

Fig. 2 Installation Type

Click on Install Now for installation and then Setup progress windows will be opened as shown in Fig. 3

Mr. P R Sali 5 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

Fig. 3 Setup Progress

After complete the installation. Click on close button in the window as shown in Fig. 4

Fig. 4: Setup Completion

Mr. P R Sali 6 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

1.4 Working with Python


Python Code Execution:
Python’s traditional runtime execution model: Source code you type is translated to byte code, which is
then run by the Python Virtual Machine (PVM). Your code is automatically compiled, but then it is
interpreted.

There are two modes for using the Python interpreter:


• Interactive Mode
• Script Mode

Running Python in interactive mode:


Without passing python script file to the interpreter, directly execute code to Python prompt. Once you’re
inside the python interpreter, then you can start.

The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python interpreter uses to
indicate that it is ready. If the programmer types 2+7, the interpreter replies 9.

>>>2+7
9
>>> print ("Welcome to Sixth Sem")
Welcome to Sixth Sem

Mr. P R Sali 7 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

Running Python in script mode:

Alternatively, programmers can store Python script source code in a file with the .py extension, and use the
interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the
interpreter the name of the file. For example, if you have a script name MyFile.py and you're working on
Unix, to run the script you have to type:
python MyFile.py

Working with the interactive mode is better when Python programmers deal with small pieces of code as
you can type and execute them immediately, but when the code is more than 2-4 lines, using the script for
coding can help to modify and use the code in future.

1.5 Python Data types:


The data stored in memory can be of many types. For example, a student roll number is stored as a numeric
value and his or her address is stored as alphanumeric characters. Python has various standard data types
that are used to define the operations possible on them and the storage method for each of them.

Int:
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20

# To verify the type of any object in Python, use the type() function:
>>> type(10)
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>

Float:
Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Float can also be scientific numbers with an "e" to indicate the power of 10.
>>> y=2.8
>>> y
2.8
>>> y=2.8
>>> print(type(y))
<class 'float'>
>>> type(.4)
<class 'float'>

Example:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))

Mr. P R Sali 8 PWP (22616)


SNJB’S SHHJB Polytechnic, Chandwad

CH.1 Intro & Syntax of Python Prog.

print(type(z))
Output:
<class 'float'>
<class 'float'>
<class 'float'>

Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>

String:
1. Strings in Python are identified as a contiguous set of characters represented in the quotation marks.
Python allows for either pairs of single or double quotes.
• 'hello' is the same as "hello".
• Strings can be output to screen using the print function. For example: print("hello").
>>> print("SNJB college")
SNJB college
>>> type("SNJB college")
<class 'str'>

Mr. P R Sali 9 PWP (22616)

You might also like