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

Python_M1_P3

The document provides an introduction to Python, highlighting its advantages such as simplicity, efficiency, and ease of learning. It covers essential topics including the Python interpreter, data types, variables, operators, and basic programming concepts. Additionally, it explains how to run Python programs in interactive and script modes, along with examples of using print and input functions.

Uploaded by

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

Python_M1_P3

The document provides an introduction to Python, highlighting its advantages such as simplicity, efficiency, and ease of learning. It covers essential topics including the Python interpreter, data types, variables, operators, and basic programming concepts. Additionally, it explains how to run Python programs in interactive and script modes, along with examples of using print and input functions.

Uploaded by

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

Introduction to Python

HUDA NOORDEAN
Department of CSE
College of Engg. Trikaripur
Why should we learn Python?

▪ Python is a higher level programming language.


▪ Its syntax allows programmers to express concepts in
fewer lines of code.
▪ Python is a programming language that lets you work
quickly and integrate systems more efficiently.
▪ One can perform symbolic mathematics easily using
Python.
▪ It is available freely online.
Searching for
Python
Downloading
Python
Running Python
There are two modes for using the Python interpreter:
1) Interactive Mode
2) Script Mode
Options for running the program:
• In Windows, you can display your folder contents, and
double click on program1.py to start the program.
• In Linux or on a Mac you can open a terminal window,
change into your python directory, and enter the command
python program1.py
Python Interpreter

The program that translates Python instructions and then executes them is the
Python interpreter. When we write a Python program, the program is executed
by the Python interpreter. This interpreter is written in the C language.
Python IDLE
Python interpreter is embedded in a number of larger programs that make it
particularly easy to develop Python programs. Such a programming environment is
IDLE
( Integrated Development and Learning Environment).
Running Python
1) In interactive mode:
>>> print("Hello Students")
Hello Students
>>> a=10
>>> print(a)
10
>>> x=10
>>> z=x+20
>>> z
30
Interactive shell
Running Python

2) In script mode:
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.

For UNIX OS to run a script file MyFile.py you have to type:


python MyFile.py
IDLE shell
Data Types
Data Types
Python has various standard data types:
▪ Integer [ class ‘int’ ] i=18
▪ Float [ class ‘float’ ] f=12.5
▪ Boolean [ class ‘bool’ ] b=True
▪ String [ class ‘str’ ] s=‘hello’
▪ Complex [ class ‘complex’] c= 2+4j
Int:
For integer or whole number, positive or negative, without decimals of
unlimited length.
>>> print(2465635468765)
2465635468765
>>> print(0b10) # 0b indicates binary number
2
>>> print(0x10) # 0x indicates hexadecimal number
16
>>> a=0b11
>>> print(type(a))
<class 'int’>
>>> print(a)
3
Float:
Float, or "floating point number" is a number, positive or negative.
Float can also be scientific numbers with an "e" to indicate the power of 10.
>>> y=2.8
>>> y
2.8
>>> print(0.00000045)
4.5e-07
>>> y=2.8
>>> print(type(y))
<class 'float'>
Boolean and String
Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
String:
>>> print(‘Science college’)
Science college
>>> type("My college")
<class 'str'>
Variables
One can store integers, decimals or characters in 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)

a= 100 # An integer assignment


b = 1040.23 # A floating point
c = "John" # A string
STATEMENTS
• A statement is an instruction that the Python interpreter can execute.
• Two kinds of statements:
1. print
2. assignment
• When you type a statement on the command line, Python executes it and displays
the result.
• The result of a print statement is a value.
Example: i=1
print i+1 Output: 2
• Assignment statements don't produce a result.
Example: x=2 Here, x is assigned with a value 2
EVALUATING EXPRESSIONS

• An expression is a combination of values, variables, and operators.


• If you type an expression on the command line, the interpreter evaluates it and
displays the result:
>>> 1 + 1
2
>>> 17
17
>>> x
2
BOOLEAN EXPRESSION
• A boolean expression is an expression that is either true or false.
• One way to write a boolean expression is to use the comparison
operator = =, which compares two values and produces a
boolean value
>>> 5 == 5
True
>>> 5 == 6
False
print function
>>>type(print)
Output:
<class 'builtin_function_or_method'>

>>>print( ‘Good morning’ ) or print(“Good morning”)


Output:
Good morning

>>>print(“Workshop”, “on”, “Python”) or print(“Workshop on Python”)


Output:
Workshop on Python
print function
>>>a=15
>>>print(a)
Output:
15
>>>b=“Hello”
>>>b=‘Hello”
>>>print(b)
Output:
Hello
print function
# printing a string
name = “Rahul”
print(‘Hey ‘ + name)
Output:
Hey Rahul
print(‘Roll No: ‘ + str(34)) # “Roll No: ” + 34 is incorrect
Output:
Roll No: 34
# printing a bool True / False
print(True)
Output:
True
print function
str1 = ‘Python code’
str2 = ‘Matlab code’
print(str1)
print(str2)
Output: Python code
Matlab code
print(str1, end=’ ‘)
print(str2)
Output: Python code Matlab code
print(str1, end=’, ‘)
print(str2)
Output: Python code, Matlab code
input function
>>>type(input)
Output:
<class 'builtin_function_or_method’>

>>>x=input() #cursor waits for user to enter input


Output:
5

>>>x=input('Enter number : ‘)
Output:
Enter number : 5
Python Keywords

Python has a set of keywords that are reserved words that cannot be
used as variable names, function names, or any other identifiers:
Python has 35 keywords:
OPERATORS AND OPERANDS
• Operators are used to perform basic
operation
>>>a=1
>>>b=2
>>>b+a
3
• Here, + is operator, a and b are operands.
• Python supports following operators

3. Logical Operators
1. Arithmetic Operators
2. Relational / Comparison Operators 4. Bitwise Operators
3. Assignment Operator 5. Membership Operators
6. Identity Operators
Arithmetic Operators
• It performs operations like addition, subtraction, multiplication, division, modulus etc.
• Assume a=10 and b=20
Relational/Comparison Operators
• These operators compare the operands like greater than, less than, equal to etc.
Assignment Operator
• Assignment operator symbol = is used in program to assign value to a variable.
• Example: x=1 means value 1 is assigned to variable x
• Similarly, x+ = 5 means x = x+5
Logical Operators

• Logical operators are used to control the flow of a program.


• The logical operators are combined with Boolean expressions.
• Three types of Logical Operators
1. and
and will result True only if both operands are True

Operand A Operand B A and B


TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
Logical Operators
2. or
or will result True if any of the operands is True
Operand A Operand B A or B
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

3. not
Operand A not A
not operator is used TRUE FALSE
to invert the truth
FALSE TRUE
table.
Bitwise Operators

• Bitwise operators manipulate directly on bits.

& Binary and

| Binary or
^ Binary Xor

~ Binary Ones compliment

<< Binary left shift

>> Binary right shift


6. Membership Operators
• It is used for testing whether the element is present or not.
• It is applied on strings, tuples, list and dictionary.
• Two membership operators are:
1. in 2. not in
Example:

>>>’h’ in “hello world”


True
>>>’a’ in “hello world”
False
>>>’a’ not in “hello world”
True
Operators
Addition + Subtraction -
Multiplication * Exponentiation **
Division / Integer division //
Remainder %
Binary left shift << Binary right shift >>
And & Or |
Less than < Greater than >
Less than or equal to <= Greater than or equal to >=
Check equality == Check not equal !=
Precedence of operators
Parenthesized expression ( ….. )
Exponentiation **
Positive, negative, bitwise not (unary operator) +n, -n, ~n
Multiplication, float division, int division, remainder *, /, //, %
Addition, subtraction +, -
Bitwise left, right shifts <<, >>
Bitwise and &
Bitwise or |
Membership and equality tests in, not in, is, is not,
<, <=, >, >=, !=, ==
Boolean (logical) not not x
Boolean and and
Boolean or or
Conditional expression if ….. else
Precedence of Operators
Example 1:

a=20 b=10 c=15 d=5 e=0


e = (a+b) * c/d
= (20+10) * (15/5) ( ) has highest priority
= 30 * 15 / 5 * / has next priority
= 450 / 5
= 90
Precedence of Operators

Example 2:

a=20 b=10 c=15 d=5 e=0


e=a+b*c
= 20 + 10 * 15 * highest priority
= 20 + 150 + next priority
= 170
Precedence of Operators
Examples:
a = 20, b = 10, c = 15 ,d = 5 ,e = 2
f = (a + b) * c / d
print( f) // 90.0

g = a + (b * c) / d – e // 48.0
print(g)

h = a + b*c**e
print(h) // 2270
Multiple Assignment
Python allows you to assign a single value to several variables
simultaneously.
a = b = c = 1.5
a, b, c = 1, 2, " Red“
Here, two integer objects with values 1 and 2 are assigned to
variables a and b respectively and one string object with the
value "Red" is assigned to the variable c.
STRINGS

• String is a collection of characters, a character represents letters and


it is enclosed with single or double quotes.
• Strings belong to compound data type in python.
• The bracket [ ] operator selects a single character from string.
>>> fruit = "banana"
>>> var = fruit[1]
>>> print var

Output: a
Special Use of + and * on strings

Examples:
print(2*' very ')
x = "Python is "
Output:
y = "awesome."
very very
z=x+y
print('It is',2*' very ','hot')
print(z)
Output:
Output:
It is very very hot.
Python is awesome.
Use of \”, \n, \t
Specifying a backslash (\) in front of the quote character in a
string “escapes” it and causes Python to suppress its usual
special meaning. It is then interpreted simply as a literal single
quote character:
>>> print(" \”Beauty of Flower\” ")
”Beauty of Flower”
>>> print('Red \n Blue \n Green ')
Red
Blue
Green
>>> print("a \t b \t c \t d")
a b c d
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 triple single quote(‘ ‘ ‘)are used for multi-line
commenting.
Example:
’’’ My Program to find
Average of three numbers ’’’
a = 29 # Assigning value of a
b = 17 # Assigning value of b
c = 36 # Assigning value of c
average = ( a + b + c)/3
print(‘Average value is ‘, average)
Thank You

You might also like