0% found this document useful (0 votes)
4 views63 pages

Class PPT Python Programming

The document provides a comprehensive guide on installing Python on Windows, including steps to check the version and download the software. It covers basic concepts of Python programming such as variable names, data types, type casting, and operators. Additionally, it emphasizes the importance of indentation and includes examples for better understanding.

Uploaded by

25au0020035
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views63 pages

Class PPT Python Programming

The document provides a comprehensive guide on installing Python on Windows, including steps to check the version and download the software. It covers basic concepts of Python programming such as variable names, data types, type casting, and operators. Additionally, it emphasizes the importance of indentation and includes examples for better understanding.

Uploaded by

25au0020035
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Anything

Can be
Taught
Anything
Can be
Learned
Getting started!!!!!!

3
Python Programming

Dr. Ashwini Jayachandran


Assistant Professor
Hindustan Institute of Technology
and science
4
Programming Language
Installation of Python in windows Dr. Ashwini /python_Programming

a) Installation of Python in
windows

Step 1: Go to search and type cmd which takes


you to the command prompt as shown here

6
Installation of Python in windows Dr. Ashwini /python_Programming

Step 2: In the command prompt


type python.

As I do not have python installed


in my system, it will take me to
microsoft store where I can get
the latest version of Python.

7
Installation of Python in windows Dr. Ashwini /python_Programming

Step 2 (a): What if I want to know the version of


python installed in my system?

Simply type

python --version

8
Installation of Python in windows Dr. Ashwini /Python_Programming

Step 2 (b): Suppose I have python installed in my system...

Then if I type Python in my command prompt, the version of python


which is intalled in the system will be displayed.

9
Installation of Python in windows Dr. Ashwini /python_Programming

Step 3:

• To download python, open


any web browser and in
your search engine type
python download.

• Then if I type ‘python


download’ something like
this apperas in my Google
(my search engine).

10
Installation of Python in windows Dr. Ashwini /python_Programming

11
Installation of Python in windows Dr. Ashwini /python_Programming

12
Installation of Python in windows Dr. Ashwini /python_Programming

13
Installation of R in windows Dr. Ashwini /python_Programming

14
Installation of Python in windows Dr. Ashwini /python_Programming

15
Installation of Python in windows Dr. Ashwini /python_Programming

16
Installation of Python in windows Dr. Ashwini /python_Programming

17
Dr. Ashwini /python_Programming

18
Python in notepad Dr. Ashwini /python_Programming

19
Python in notepad Dr. Ashwini /python_Programming

• Save the notepad with .py file extension


• Go to command prompt
• Type python and drag the text file into it.
• Then the path will be displayed as shown here
• Press enter to display the output of the program in the notepad
20
Installation of Python in windows Dr. Ashwini /python_Programming

Without installing can I do


python programming??

21
Installation of Python in windows Dr. Ashwini /python_Programming

22
Installation of Python in windows Dr. Ashwini /python_Programming

23
Installation of Python in windows Dr. Ashwini /python_Programming

24
Installation of Python in windows Dr. Ashwini /python_Programming

25
Installation of Python in windows Dr. Ashwini /python_Programming

26
Installation of Python in windows Dr. Ashwini /python_Programming

27
Installation of Python in windows Dr. Ashwini /python_Programming

28
Installation of Python in windows Dr. Ashwini /python_Programming

• IDE stands for Integrated


Development Environment.

• They are software built for


writing, debugging, compiling and
running code.

• They usually come with powerful


debugging kits, for watching or
stepping through your code.

29
Installation of Python in windows Dr. Ashwini /python_Programming

30
Translator

When you learn a new language, you need


that language translator.
Why python?

• Interpreted, Interactive, Object-Oriented, Open-Source, Easy To


• Learn, Easy To Read, Easy To Type, Portable, Extendable, Rich In
• Packages.
• Three variations: IDLE (GUI),
python (command line), Colab (online)
• Great for learning the language
• Great for experimenting with the library
• Great for testing your own modules
Display

print 1+3

pi = 3.1415926
print pi

message = "Hello, world"


print message

Output:
4
3.1415926
Hello, world
Variable names (Identifiers)

1. It should use only alphabets (upper & lower case English alphabets and
characters of other world languages), digits & underscore ‘_’. (We cannot use
special symbols like !,@, #, $, % etc).

• 2. It should not begin with a digit and must have at least one alphabet.

• 3. It can be of any length (Do not overuse this provision !)

• 4. It should not be a reserved word (a word which has a special meaning in


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:
• These are the examples of reserved or keywords used in
Python
and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield
Variable names

Python will tell you if an identifier you plan to use is valid


using isidentifier()

• For example,

• 'A/C_number'.isidentifier()

• returns False
Data types

The basic variable types in python are


o Integer
o Float
o String
o Boolean
Data types: Integer
Integer (No decimal part)
 a = 5

 b = -250

 student_id = 25697

 क = 5

Integers have no size limit.


 x=5**100000;

 print(x)

 100099890379869416681626471319330624849934750830578004920

283338007975850731624625203823576404317149791344357367267
3571911170872235539685391574584515464760420618409886836346
812336096308354237379904201042262666868044835290433422580
1019821028944489915570708690166060581976061571940488574128
530587342639885597733941379909951118…. <EDITED HERE> >>>>>>>It
will run into pages.
Data types: Float
These are numbers with decimal part. When such numbers are too small or
too big, they are expressed in a special format

Float (Decimal part present)

interest_rate = 3.5
height = 1.6
x = -1027.683
ന = 0.67
Data types: Character
• Characters (Actually characters are merely strings with just one element,
• we will learn about strings in a later chapter).

• A character is any single symbol you see on the keyboard.

• They are enclosed with single or double quotes. Examples are: x=’a’, y=’1’,
• z=’+’ etc.
Data types: Character
• Characters have number codes (Unicode) behind them.

• You can see them using ord() Answers:


• x='a'
• print(ord(x)) 97

• x='A' 65
• print(ord(x))

• x=' ₹'
• print(ord(x)) 8377
Data types: Strings
"hello"+"world" "helloworld" # concatenation
"hello"*3 "hellohellohello" # repetition
"hello"[0] "h" # indexing
"hello"[-1] "o" # (from end)
"hello"[1:4] "ell" # slicing
len("hello") 5 # size
"hello" < "jello" 1 # comparison
"e" in "hello" 1 # search
Data types: Boolean
Python boolean type is one of the built-in data types provided by Python, which represents one of the
two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.

Example
Input: 1==1
Output: True

Input: 2<1
Output: False

The output <class ‘bool’> indicates the variable is a boolean data type.

a = True
type(a)

b = False
type(b)
types

Examples
pi = 3.1415926
message = "Hello, world"
i = 2+2

print type(pi)
print type(message)
print type(i)

Output:
<type 'float'>
<type 'str'>
<type 'int'>
Type Casting in Python (Implicit and Explicit) with
Examples

 Type Casting is the method to convert the Python variable datatype into
a certain data type in order to perform the required operation by users.

 In this article, we will see the various techniques for typecasting. There
can be two types of Type Casting in Python:

 Python Implicit Type Conversion


 Python Explicit Type Conversion
Implicit Type Conversion in Python
In this, method, Python converts the datatype into another datatype automatically. Users
don’t have to involve in this process.
# Python automatically converts a to int
a=7
print(type(a))

# Python automatically converts b to float


b = 3.0
print(type(b))

# Python automatically converts c to float as it is a float addition


c=a+b
print(c)
print(type(c))
Explicit Type Conversion in Python
In this method, Python needs user involvement to convert the variable data type into the
required data type.

Examples of Type Casting in Python. Mainly type casting can be done with these data type
functions:

Int(): Python Int() function take float or string as an argument and returns int type object.

float(): Python float() function take int or string as an argument and return float type
object.

str(): Python str() function takes float or int as an argument and returns string type
object.
Explicit Type Conversion in Python
# Python program to demonstrate type Casting

# int variable
a=5

# typecast to float
n = float(a)

print(n)
print(type(n))

Output:

5.0
<class 'float'>
Python input

x=input()
• print(x)

Whatever is typed by the user is accepted as a string. If it is


meant to be a number, use int( ) or float() to convert them.

N=input( ) N=int(input( ))
for i in range(1,N): for i in range(1,N):
print(i) print(i)

If Input is 5, then output will be


Error ! 1,2,3,4
end=’ ‘ in python
• The end parameter in the print function is used to add any
string at the end of the output of the print statement in python.

• By default, the print function ends with a newline.

• Passing the whitespace to the end parameter (end=‘ ‘) indicates


that the end character has to be identified by whitespace and
not a newline.
Example of end=’ ‘ in python
for i in range(1,5): for i in range(1,5):
print(i) print(i, end=’ ‘)

1234
1
2
3
4
Escape sequences
Escape Sequences are characters that don’t print anything, but have an
effect. Try it!!!
\b back space
it will delete all that is printed !
\t for Tab
for i in range(0,10):
print(i, end='\t')
\a for alert
for i in range(0,10):
print(i, end='\a')
\n for newline
for i in range(0,10):
print(i, end='\n')
Indentation in Python
Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for


readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

Example
if 5 > 2:
print("Five is greater than two!")
Indentation in Python
Syntax Error:

if 5 > 2:
print("Five is greater than two!")

if 5 > 2:
print("Five is greater than two!")

if 5 > 2:
print("Five is greater than two!")
Consistency is important in the Indentation
in Python
You have to use the same number of spaces in the same block of code,
otherwise Python will give you an error:

Example
Syntax Error:

if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Operators: Arithmatic
 + addition
 - subtraction

 *multiplication

 / division

 ** exponentiation

 % modulus (remainder after division)


Examples

print 2*2
print 2**3
print 10%3
print 1.0/2.0
print 1/2

Output:
4
8
1
0.5
0
 Note the difference between floating point division and integer division in
the last two lines
Type Conversion

int(), float(), str(), and bool() convert to integer, floating point,


string, and boolean (True or False) types, respectively

 Example:

Predict the

print 1.0/2.0
print 1/2
print float(1)/float(2)
output..
print int(3.1415926)
print str(3.1415926)
print bool(1)
print bool(0)
Operators: Relational

== : is equal to?
 != : not equal to
 > : greater than
 < : less than
 >= : greater than or equal to
 <= : less than or equal to
Operators: Logical

and, or, not

>>> 2+2==5 or 1+1==2


True
>>> 2+2==5 and 1+1==2
False
>>> not(2+2==5) and 1+1==2
True
Practice Problem

Write a Python program that inputs three different integers
from the keyboard, and then print the sum, the average and the
product of these numbers. Display the information as below:

 Input three different integers:


 13
 27
 14
Output

Sum is 54

 Average is 18

 Product is 4914

You might also like