IPP Unit 1 Note

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Unit 1

Introduction to Python

Python is an interpreted, high-level, general-purpose programming language. Created by Guido


van Rossum and first released in 1991, Python has a design philosophy that emphasizes code
readability, notably using significant whitespace. It provides constructs that enable clear
programming on both small and large scales. Van Rossum led the language community until July
2018

Python is dynamically typed and garbage-collected. It supports multiple programming


paradigms, including procedural, object-oriented, and functional programming. Python features a
comprehensive standard library, and is referred to as "batteries included"

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.

2. Expressive Language:-Python language is more expressive means that it is more


understandable and readable.

3. Interpreted Language:-Python is an interpreted language i.e. interpreter executes the


code line by line at a time. This makes debugging easy and thus suitable for beginners.

4. Cross-platform Language:-Python can run equally on different platforms such as


Windows, Linux, Unix and Macintosh etc. So, we can say that Python is a portable
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.

6. Object-Oriented Language:-Python supports object oriented language and concepts of


classes and objects come into existence.

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.

9. GUI Programming Support:-Graphical user interfaces can be developed using Python.

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:

 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)
e.g. x = 4 # x is of type int
x = "Sally" # x is now of type str

Remember that variables are case-sensitive

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

1) Python Arithmetic 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

2) Python Assignment Operators


Assignment operators are used to assign values to variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
3) Python Comparison Operators

Comparison operators are used to compare two values

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

6) Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:


Operator Description Example
Returns True if a sequence with
in  the specified value is present in x in y
the object
Returns True if a sequence with the
not in x not in y
specified value is not present in the object

7) Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:


Operator Name Description
&  AND Sets each bit to 1 if both bits are 1
Sets each bit to 1 if one of two
| OR
bits is 1
 ^ XOR Sets each bit to 1 if only one of two bits is 1
~  NOT Inverts all the bits
Shift left by pushing zeros in from the right and let
<< Zero fill left shift
the leftmost bits fall off
Shift right by pushing copies of the leftmost bit in
>> Signed right shift
from the left, and let the rightmost bits fall off
Python Program for addition of two numbers

Num1=int(input())
Num2=int(input())

Add=Num1+Num2

print (Add)

You might also like