Working with Numbers &
String in Python
Python Numbers
Python numbers are a fundamental data type
for representing and manipulating numerical
values.
Three numeric Data types are,
1. int or Integer
2. float or Floating
3. complex
1. int or Integer
int is a whole number without decimals.
E.g:
x = 12
y=7
print(type(x))
print(type(y))
Output:
<class 'int'>
<class 'int'>
2. float or Floating
A Number with one or more decimal values.
E.g:
x = 12.4
y = 42E
print(type(x))
print(type(y))
Output:
<class 'float'>
<class 'float'>
3. complex
A Numbers with imaginary part is a complex
number.
E.g:
x = 3 + 2j
y = -15j
print(type(x))
print(type(y))
Output:
<class 'complex'>
<class 'complex'>
Number Type conversion
To convert numbers from one type to another type with the
1. int()
2.float()
3.complex()
E.g:
x = 4 # int number
y = 0.8 # float number
z = 4j # complex number
# from int to float:
a = float(x)
print(a)
Python Strings
A string is a sequence of characters enclosed
in single quotes (''), double quotes (“”)or
Multiline(“”””””).
E.g:
name = “Priya"
role = "Software Engineer"
print(name)
print(role)
Working with Strings
Checking the String Length
len() function
E.g:
name = "python“
length_of_name = len(name)
print(length_of_name)
Output:
6
Checking an character in a string
Using the in keyword.
E.g:
sentence = "python programming language"
if "python" in a sentence:
print("Yes, 'python' is present.")
String Methods
capitalize() -method converts the first character of a string
to an uppercase letter and lowercases all other characters.
Upper()-method converts all the characters into Uppercase.
Lower()-method converts all the characters into lowercase.
Replace()-is used to replace a string with another.
Find()-searches the string for a specified value and returns
the position of where it was found.
E.g:
name = "PYTHON"
print(name.lower())
Output:
python
Python Functions
Python Functions is a block of code. It run
whenever its call.Data is passed as an
argument or parameter to the function.
Benefits:
Increase Code Readability
Increase Code Reusability
Function Declaration
Creating a Function in Python
Define a function in Python, using
the def keyword.
Colon mark indicates the end of function
header.
E.g:
def fun():
print("Welcome to vcas")
Types of Functions in Python
Built-in library function: These are
Standard functions in Python that are
available to use.
E.g:
set()
int()
float()
User-defined function: We can create our
own functions based on our requirements.
E.g:
# Program to illustrate
# the use of user-defined functions
def add_numbers(x,y):
sum = x + y
return sum
num1 = 5
num2 = 6
print("The sum is", add_numbers(num1, num2))
Calling a Function in Python
After creating a function in Python we can
call it by using the name of the functions
Python followed by parenthesis containing
parameters of that particular function.
E.g:
def college():
print("Welcome to vcas")
# to call a function
college()
THANKYOU