Unit1 - Introduction To Python
Unit1 - Introduction To Python
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. 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.
4. It’s Powerful
Dynamic typing
Built-in types and tools
Library utilities
Third party utilities (e.g. Numeric, NumPy, sciPy)
Automatic memory management
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.
7. Interpreted Language
Python is processed at runtime by python Interpreter
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)
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
>>> 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.
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/
Click on Download Python 3.9.2 installation window will be open as shown in Fig 2.
Click on Install Now for installation and then Setup progress windows will be opened as shown in Fig. 3
After complete the installation. Click on close button in the window as shown in Fig. 4
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
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.
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))
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'>