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

Introduction to Python-day 3 (1)

The document provides an introduction to Python, focusing on data types, type conversion, and operators. It explains Python's dynamic typing, numeric types, and methods for type conversion, including implicit and explicit conversions. Additionally, it covers string manipulation, slicing, and various operators used in Python programming.

Uploaded by

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

Introduction to Python-day 3 (1)

The document provides an introduction to Python, focusing on data types, type conversion, and operators. It explains Python's dynamic typing, numeric types, and methods for type conversion, including implicit and explicit conversions. Additionally, it covers string manipulation, slicing, and various operators used in Python programming.

Uploaded by

hema.docs88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

INTRODUCTION TO PYTHON

Data Types in Python:

Python supports various data types, these data types defines the operations
possible on the variables and the storage method.
▪ Variables can hold values of different data types.
Python is a dynamically typed language hence
we need not define the type of the variable while
declaring it.
▪ The interpreter implicitly binds the value with its
type.
▪ Python enables us to check the type of the variable
used in the program.
▪ Python provides us the type() function which
returns the type of the variable passed.
▪ A=10
▪ b="Hi Python"
▪ c = 10.5
▪ print(type(a));
▪ print(type(b));
▪ print(type(c));
Python Numbers
There are three numeric types in Python:
▪ int
▪ float
▪ complex
Eg:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Type Conversion

• The process of converting the value of one data type (integer,


string, float, etc.) to another data type is called type
conversion. Python has two types of type conversion.
• Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion
• In Implicit type conversion, Python automatically converts one
data type to another data type. This process doesn't need any
user involvement.
Explicit Type Conversion

• In Explicit Type Conversion, users convert the data type of an object to required
data type. We use the predefined functions like int(), float(), str(), etc to perform
explicit type conversion.
• This type of conversion is also called typecasting because the user casts (changes)
the data type of the objects.
• Syntax :
• <required_datatype>(expression)
Type Conversion
x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)
print(a)
print(b)
print(c)
Python Casting
▪ x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
▪ x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
▪ x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Rounding: floor, ceil, trunc
FLOOR
• get the largest integer less than x
Eg :x = 1.55 y = -1.55,
math.floor(x) # 1
math.floor(y) # -2
CEIL
• get the smallest integer greater than x
• Eg : x = 1.55 y = -1.55
math.ceil(x) # 2
math.ceil(y) # -1
TRUNC
• drop fractional part of x
• Eg : x = 1.55 y = -1.55
math.trunc(x) # 1,
math.trunc(y) # -1,
Rounding: round

• Python provides an inbuilt function round() which rounds off


to the given number of digits and returns the floating point
number, if no number of digits is provided for round off , it
rounds off the number to the nearest integer.
• SYNTAX
• round(number, number of digits)
• print(round(2.665, 2)) # 2.67
• print(round(2.676, 2)) #2.68
• print(round(2.673, 2)) #2.67
Comments
1) Single Line Comment:
# This is single line comment.
print "Hello Python"
2)Multi Line Comment:
''' This
Is
Multipline comment'''
Input
Syntax:
input([prompt])

>>> num = input('Enter a number: ')


Enter a number: 10
>>> num
'10‘
>>> int(num)
10
Output Formatting
▪ Sometimes we would like to format our output to make it
look attractive. This can be done by using
the str.format() method. This method is visible to any string
object.
▪ >>> x = 5; y = 10
▪ >>> print('The value of x is {} and y is{}'.format(x,y))
▪ The value of x is 5 and y is 10
Output Formatting
age = 20
txt = "My name is arun, and I am {}"
print(txt.format(age))
▪ print('Hello {name},
{greeting}'.format(greeting =
'Goodmorning', name =
'John'))
▪Hello John, Goodmorning
Python Operators
Operators are
special symbols in
Python that carry
out arithmetic or
logical
computatiosn. The
value that the
operator operates
on is called the
operand.
Arithmetic operators
Relational Operators
Logical operators
Truth Table
ASSIGNMENT OPERATORS
Operator Example Equivlatent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Bitwise operators
X= 10, Y= 4
Membership operators
Membership Operator
x = ‘Good Morning'
y = {3:'a',4:'b'}

print('G' in x) # True
print('good' not in x) #True
print('Good' not in x) #False
print(3 in y) #True
Identity Operators
▪ is and is not are the identity operators in
Python. They are used to check if two
values (or variables) are located on the
same part of the memory. Two variables
that are equal does not imply that they are
identical.
Identity Operators
String
▪ Creating a String

To create a string in Python we need to use


either single quotes or double or triple quotes
▪ print("Hello")
print('Hello')
Multiline Strings
a = ""“Welcome to Kumarasamy college
of Engineering karur."""

print(a)
Strings are Arrays
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])
Slicing
▪ You can return a range of characters by using the slice syntax.
▪ Specify the start index and the end index, separated by a colon, to return a
part of the string.
▪ start_pos default value is 0, the end_pos default value is the length of string
and step default value is 1.

b = "Hello, World!"
print(b[2:5])
# output:llo
Get the characters from position 2 to position 5 (not included)
slice() Parameters
▪ slice() mainly takes three parameters which have the same
meaning in both constructs:
▪ start - starting integer where the slicing of the object starts
▪ stop - integer until which the slicing takes place. The slicing stops
at index stop - 1.
▪ step - integer value which determines the increment between
each index for slicing
a = [1, 2, 3, 4, 5, 6, 7, 8]
print(a[:5]) # prints [1, 2, 3, 4, 5]
print(a[2:]) # prints [3, 4, 5, 6, 7, 8]
print(a[2:5]) # prints [3, 4, 5]
print(a[2:7:2]) # prints [3, 5, 7]

You might also like