Session1: Installation, Data Types, Variables

You might also like

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

Session1: Installation, Data types,

Variables
Installation:

Installation could be done from Python.org

Use MSI installer to select for 32 bit or 64 bit version

Expression: an expression is a combination of values and operators

>>>8+9

17

in python the 8+9 is an expression

Different mathematical operators

+ (addition)

- (subtraction)

%(Remainder or modulus)

/ (division)

* (multiplication)

// (quotient)

Order of preference BODMAS rule and for equivalent operators from left to right.

>>> a=5

>>> a

>>> a*3
15

>>> a/5

1.0

>>> a

>>> a=12.5

>>> a

12.5

>>>

>>> a=35

>>> b=25

>>> a=4

>>> b=2

>>> a/b

2.0

>>> a//b

>>> a%b

>>> 2**3

>>> 2*3

6
>>> a=5

>>> b=2

>>> a+b

>>> c=a+b

>>> c

Evaluating an expression

>>> 2+3*6

20

>>> 2+6/2-2

3.0

>>> 6-2+9/3*6

22.0

# % and // are having the same preference

>>> 3+4%2

>>> 3//4%2

>>> 1-3//4%2

DATA TYPE
Most commonly used data types are

1) Integer

2) Float

3) Strings

 Integer --> (-infinity to +infinity)

>>>a=10

>>>type(a)

 Float -->(decimal numbers)

>>>a=15.6

>>>type(a)

float

 Strings --->(group of alphabets)

>>>a='yashwanth'

>>>type(a)

str

Integers can be converted to a string but not vice versa

>>> a=8

>>> str(a)

'8'

>>> type(a)

<class 'int'>

>>> a=8

>>> b=str(a)
>>> b

'8'

>>> type(b)

<class 'str'>Exceptions:

We cannot add an integer to a string

>>> a='yashwanth'

>>> a+10

Traceback (most recent call last):

File "<pyshell#15>", line 1, in <module>

a+10

TypeError: Can't convert 'int' object to str implicitly

#Python is case sensitive language

a=int(input('enter the value of a '))


b=float(input('enter the value of b '))

c=A+b

print(c)

STRINGS:
a string is a group of alphabets or words

Replication of strings

>>> a*10
'yashwanthyashwanthyashwanthyashwanthyashwanthyashwanthyashwanthyashwa
nthyashwanthyashwanth'

Concatenation of two strings

>>> 'hello'+'good morning'

'hellogood morning'

>>> 'hellogood morning'

'hellogood morning'

>>> a='iglobal'

>>> a[1]

'g'

>>> a[2]

We cannot multiply two strings

>>>'good'*'morning'

Traceback (most recent call last):

File "<pyshell#22>", line 1, in <module>

'good'*'morning'

TypeError: can't multiply sequence by non-int of type 'str'

Input Function

Syntax:Input('enter the value')


Input function takes the input by default as string

#Program to concatenate and replicate the strings

a=input('enter the value of a ')

b=input('enter the value of b ')

c=a+b

r=c*5

print(c,',',r)
if we would like to convert integer input, we have to use

n=int(input('enter the value of string')

then the input becomes integer


>>> a=int(input('cricket football '))

cricket football 10

>>> a

10

>>> a=float(input('cricket football '))

cricket football 10

>>> a

10.0
FLOAT DATA TYPE INPUT

#Program to add integer and float numbers

a=int(input('enter the value of a '))

b=float(input('enter the value of b '))

c=a+b

print(c)
#sample program to divide integer and float numbers

a=int(input('enter the value of a '))

b=float(input('enter the value of b '))

c=int(a/b)

print(c)

You might also like