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

Python Unit - 2

This document provides an overview of integers, floating-point numbers, and complex numbers in Python, detailing their definitions, examples, and associated functions. It also covers Python operators, including arithmetic, bitwise, and comparison operators, as well as numeric type functions and sequence types like strings, lists, and tuples. Additionally, it explains various string functions and methods for manipulating lists and tuples.

Uploaded by

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

Python Unit - 2

This document provides an overview of integers, floating-point numbers, and complex numbers in Python, detailing their definitions, examples, and associated functions. It also covers Python operators, including arithmetic, bitwise, and comparison operators, as well as numeric type functions and sequence types like strings, lists, and tuples. Additionally, it explains various string functions and methods for manipulating lists and tuples.

Uploaded by

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

UNIT – 2 INTEGERS

Numbers – Introduction to Numbers – Integers – Double precision  This contains Whole number
floating point numbers – Complex numbers – Operators – Numeric type Example:
functions – Sequences: Strings, Lists and Tuples – Sequences – Strings a=5
and strings operators – String built-in methods – Lists – List type Built print ("The type of a", type(a))
in Methods – Tuples. Output:
The type of a <class 'int'>
DATA TYPES 2. int () Function
 It converts the string into integer.

Syntax
int (value, base)
Example:
a=int(input(“Enter the Value: ”))
FLOATING POINT NUMBERS
 This contains real numbers.
Example:
b = 40.5
print("The type of b", type(b))
NUMBERS Output:
Introduction to Numbers The type of b <class 'float'>
 Numeric values are stored in numbers. float () Function
 Python supports three kinds of numerical data.  It converts the string into Floating point.
o int Syntax
o float float (value, base)
o complex Example:
a=float(input(“Enter the Value: ”))

V. Naresh Kumar, M.C.A., M.Phil., Page 1


Double-Precision Floating-Point Number COMPLEX NUMBER
 Commonly used in computing to represent decimal number.  This contains an arranged pair.
 It is represented using 64 bits Example
 Double-precision numbers is approximately 15 to 17 decimal c = 1+3j
digits. print("The type of c", type(c))
 Double-precision numbers suitable for applications of Output:
Scientific calculations, financial computations, and other The type of c <class 'complex'>
scenarios c is complex number: True.

 Here, bits are typically divided into three components:


1. Sign
2. Exponent
3. Fraction
1. Sign
Determines whether the number is positive or negative.
2. Exponent
Represents the power.
3. Fraction
Holds the significant bits of the number.

V. Naresh Kumar, M.C.A., M.Phil., Page 2


PYTHON OPERATORS 4. Assignment Operators

 It performs a specific operation between two operands.  Value is assigned to the left operand.

1. Arithmetic Operators 5. Logical Operators


Addition : It is used to add two operands.  Logical operators are and, or, and not.
Subtraction : It is used to subtract the second operand. Name of
Operator Meaning
Operator
Divide : Dividing the first operand by the second.
&& And Both operands are true.
It returns the quotient.
|| Or Any one operand is true
Multiplication : Multiply one operand with the other.
2. Bitwise Operators ! not Operand is false
Example:

3. Comparison operators
 Compare the values of the two operands.
Operator Name of Operator Meaning
== Equal Check the two operands are equal
OUTPUT
!= Not Equal Check the two operands are not equal

<= Less than (or) Equal Check the operand is less than or equal

Greater than (or) Check the operand is greater than


>=
Equal or equal
> Greater than Check the operand is greater than

< Less than Check the operand is less than

V. Naresh Kumar, M.C.A., M.Phil., Page 3


NUMERIC TYPE FUNCTIONS 4. Round:

Integer Operations:  Round(x, n): rounds 'x' to 'n' decimal places.


1. Basic Arithmetic Operations: Complex Number Operations:
 Addition: a + b 1. Basic Operations:

 Subtraction: a - b  Addition: a + b

 Multiplication: a * b  Subtraction: a - b

 Division: a / b & Modulo: a % b (returns the remainder)  Multiplication: a * b

2. Exponentiation:  Division: a / b

 Power: a ** b (raises 'a' to the power of 'b') Common Functions:

3. Conversion Functions: 1. Absolute Value:

 int(x): converts x to an integer. abs(x):

 float(x): converts x to a floating-point number.  returns the absolute value of 'x'.

 complex(real, imag): creates a complex number. 2. Maximum and Minimum:

Floating-Point Operations: max(x, y, ...):

1. Basic Arithmetic Operations:  Returns the maximum value among the arguments.

 Addition: a + b min(x, y, ...):

 Subtraction: a - b  Returns the minimum value among the arguments.


3. Type Checking:
 Multiplication: a * b
isinstance(x, type):
 Division: a / b
 Checks if 'x' is an instance of the specified 'type'.
2. Exponentiation:
4. Type Conversion:
 Power: a ** b (raises 'a' to the power of 'b')
 int(x), float(x), complex(x): convert 'x' to an integer,
3. Conversion Functions:
float, or complex number.
 int(x): converts x to an integer.
5. Math Module:
 float(x): converts x to a floating-point number.
 Contains various mathematical functions.
 complex (real, imag): creates a complex number.
e.g., math.sqrt (), math.sin (), math.cos (), etc.

V. Naresh Kumar, M.C.A., M.Phil., Page 4


SEQUENCE 3. [ ]
String:  It is a slice operator.
 Quotation marks can be used to describe the string.  Used to access the sub-strings of a particular string.
 A string can be defined using 4. [:]
o Single.  It is a range slice operator.
o Double (Or) Triple quotes.  Used to access the characters from the specified range.
Example: 5. in
str = "string using double quotes"  It is a membership operator.
print(str)  It returns if a particular sub-string is present in the specified
STRINGS OPERATOR string.
6. not in
 It is a membership operator.
 It returns true if a particular substring is not present in the
specified string.
7. 8. %
 Used to perform string formatting.
Example:

1. + str1 = "Hello"

 It is a concatenation operator str2 = " World"

 Used to join the strings. print(str1*3) # prints HelloHelloHello

2. * print(str1+str2) # prints Hello world


print(str1[4]) # prints o
 It is a repetition operator.
print(str1[2:4]) # prints ll
 Concatenates the multiple copies of the same string.

V. Naresh Kumar, M.C.A., M.Phil., Page 5


STRING FUNCTIONS 4. Join ()
1. Len ()  Returns a new string which is the concatenation of the
 Returns the length of the string. strings.
Syntax: Syntax:
len(string) str.join(sequence)
Example: Example:
str1="Python Language" str1 = ":"
print (len(str1)) str2 = "NK"
2. Lower () str3= str1.join (str2)
 Converts the uppercase character to lowercase. print (str3)
Syntax: 5. islower()
str.lower()  Returns True if all characters in the string are in lowercase.
Example:  It returns False if not in lowercase.
str=”PyTHOn” Syntax:
print(str.lower()) str.islower()
3. Upper () str1 = "python"
 Converts the lowercase character to uppercase. print(str1.islower())
Syntax: 6. isupper ()
str.upper()  Returns True if all characters in the string are in uppercase.
Example:  It returns False if not in uppercase.
str=”PyTHOn” Syntax:
print(str.upper()) str.isupper()
Example:
str1 = "PYTHON"
print(str1.isupper())

V. Naresh Kumar, M.C.A., M.Phil., Page 6


LIST List Length
 Used to store multiple items in a single variable.  Determines the no. of item in the list.
 Lists are created using square brackets.  It has the len() method.
Example Built - in method
thislist = ["apple", "banana", "cherry"] Append ():
print(thislist) Adds an element to the end of the list.
List Items Insert ():
 List items are Inserts an element at a specified position in the list.
1. Ordered Remove ():
2. Changeable Removes the first occurrence of an element from the list.
3. Duplicate values. Pop ():
1. Ordered Removes and returns the element at a specified index.
 Defined order.
 Order will not change.
 Add the new item end of the list.
2. Changeable
 Change, add, and remove items in a list
3. Duplicates
 Lists can have the same value

V. Naresh Kumar, M.C.A., M.Phil., Page 7


TUPLES Tuple Length
 Used to store multiple items in a single variable.  To determine no. of items a tuple has.
 It is the built-in data types.  It use the len() function.
 Store the collections of data Example
 It maybe is an ordered and unchangeable. thistuple = ("apple", "banana", "cherry")
 Tuples are written with round brackets. print(len(thistuple))
Example
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple Items
Tuple items are
1. Ordered
2. Unchangeable
3. Duplicate values.
Ordered
 Tuples items are in ordered.
 The order will not change
Unchangeable
 Here, we cannot change (add or remove) the items order.
Allow Duplicates
 Here, the items with same value
Example:
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)

V. Naresh Kumar, M.C.A., M.Phil., Page 8

You might also like