IPP Unit 1 Note
IPP Unit 1 Note
IPP Unit 1 Note
Introduction to Python
Feature of Python:-
1. Easy to Learn and Use: - Python is easy to learn and use. It is developer-friendly and
high level programming language.
5. Free and Open Source:-Python language is freely available at offical web address.The
source-code is also available. Therefore it is open source.
7. Extensible:-It implies that other languages such as C/C++ can be used to compile the
code and thus it can be used further in our python code.
8. Large Standard Library:-Python has a large and broad library and prvides rich set of
module and functions for rapid application development.
10. Integrated:-It can be easily integrated with languages like C, C++, JAVA etc.
Python blocks Python uses a different principle. Python programs get structured through
indentation, i.e. code blocks are defined by their indentation. Okay that's what we expect
from any program code, isn't it? Yes, but in the case of Python it's a language requirement
not a matter of style. This principle makes it easier to read and understand other people's
Python code. So, how does it work? All statements with the same distance to the right belong
to the same block of code, i.e. the statements within a block line up vertically. The block
ends at a line less indented or the end of the file. If a block has to be more deeply nested, it is
simply indented further to the right.
Python variables Python is dynamically typed, which means that you don't have to declare what
type each variable is. In Python, variables are a storage placeholder for texts and numbers. It
must have a name so that you are able to find it again. The variable is always assigned with the
equal sign, followed by the value of the variable. A variable can have a short name (like x and y)
or a more descriptive name (age, carname, total_volume). Rules for Python variables:
Comments: - Single-line comments are created simply by beginning a line with the hash (#)
character, and they are automatically terminated by the end of line.
For example: #This would be a comment in Python
Comments that span multiple lines – used to explain things in more detail – are created by
adding a delimiter (“””) on each end of the comment.
“””” This would be a multiline comment
in Python that spans several lines and
describes your code, your day, or anything you want it to
…
“””
Python Data Types: Python has five standard Data Types:
Numbers
String
List
Tuple
Dictionary
1) Numbers: - Python numbers variables are created by the standard Python method: var = 382
Most of the time using the standard Python number type is fine. Python will automatically
convert a number from one type to another if it needs. But, under certain circumstances that a
specific number type is needed (ie. complex, hexidecimal), the format can be forced into a
format by using additional syntax in the table below:
Type Format Description
int a = 10 Signed Integer
long a = 345L (L) Long integers, they can also be represented in octal and hexadecimal
float a = 45.67 (.) Floating point real values
complex a = 3.14J (J) Contains integer in the range 0 to 255.
Most of the time Python will do variable conversion automatically. You can also use Python
conversion functions (int(), long(), float(), complex()) to convert data from one type to
another. In addition, the type function returns information about how your data is stored
within a variable.
2) Strings: - String literals in python are surrounded by either single quotation marks, or double
quotation marks.'hello' is the same as "hello".Strings can be output to screen using the print
function. For example: print("hello"). Like many other popular programming languages,
strings in Python are arrays of bytes representing unicode characters. However, Python does
not have a character data type, a single character is simply a string with a length of 1. Square
brackets can be used to access elements of the string.
a = "Hello, World!" print(a[1])
Concatenation
In Python, there are a few ways to concatenate – or combine - strings. The new string that is
created is referred to as a string object. Obviously, this is because everything in Python is an
object – which is why Python is an objected-oriented language.
In order to merge two strings into a single object, you may use the “+” operator. When writing
code that would look like this:
str1 = “Hello”
str2 = “World”
str1 + str2
Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Operator Name
Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
4) Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example
Returns True if both statements
and x < 5 and x < 10
are true
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
5) Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually
the same object, with the same memory location:
Operator Description Example
Returns true if both variables are
is x is y
the same object
Returns true if both variables are not
is not x is not y
the same object
Num1=int(input())
Num2=int(input())
Add=Num1+Num2
print (Add)