Type Conversion in Python
Type Conversion in Python
The act of changing an object’s data type is known as type While Loop in Python
conversion. The Python interpreter automatically performs
In Python, a while loop is used to execute a block of
Implicit Type Conversion. Python prevents Implicit Type
statements repeatedly until a given condition is satisfied.
Conversion from losing data.
When the condition becomes false, the line immediately
Implicit Type Conversion in Python after the loop in the program is executed.
In Implicit type conversion of data types in Python, the
count = 0
Python interpreter automatically converts one data type to
while (count < 3):
another without any user involvement. To get a more clear
count = count + 1
view of the topic see the below examples.
print("Hello Geek") o/p : Hello Geek
x = 10
Hello Geek
print("x is of type:",type(x))
Hello Geek
y = 10.6
For Loop in Python :
print("y is of type:",type(y))
For loops are used for sequential traversal. For example:
z=x+y traversing a list or string or array etc.
n=4
print(z)
for i in range(0, n):
print("z is of type:",type(z)) print(i) o/p : 0
1 2 3
o/p : x is of type: <class 'int'> Nested Loops in Python
y is of type: <class 'float'>
20.6 Python programming language allows to use one loop inside
z is of type: <class 'float'> another loop which is called nested loop. Following section
shows few examples to illustratconconcept. from
Explicit Type Conversion in Python __future__ import print_function
In Explicit Type Conversion in Python, the data type is for i in range(1, 5):
manually changed by the user as per their requirement. With
explicit type conversion, there is a risk of data loss since we for j in range(i):
are forcing an expression to be changed in some specific data
print(i, end=' ')
type.
print() o/p: 1
# initializing string
22
s = "10010"
333
# printing string converting to int base 2
4444
c = int(s,2)
Loop Control Statements
print ("After converting to integer base 2 : ", end="")
Continue Statement
print (c)
The continue statement in Python returns the control to the
# printing string converting to float
beginning of the loop.
e = float(s)
for letter in 'geeksforgeeks':
print ("After converting to float : ", end="")
if letter == 'e' or letter == 's':
print (e) o/p: After converting character to integer : 52
continue
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70 print('Current Letter :', letter) o/p: Current Letter : g
Current Letter : k
Current Letter : f print("\nType of c: ", type(c)) 2.
2..Sequence Data Types in Python
Current Letter : o
The sequence Data Type in Python is the ordered collection
Current Letter : r
of similar or different Python data types.
Current Letter : g
Strings in Python are arrays of bytes representing Unicode
Current Letter : k characters. A string is a collection of one or more characters
put in a single quote, double-quote, or triple-quote.
Break Statement
List Data Type
The break statement in Python brings control out of the loop.
Lists are just like arrays, declared in other languages which is
for letter in 'geeksforgeeks':
an ordered collection of data. It is very flexible as the items
if letter == 'e' or letter == 's': in a list do not need to be of the same type.
print('Current Letter :', letter) Lists in Python can be created by just placing the sequence
inside the square brackets[].
o/p: Current Letter : e
# Empty list
Pass Statement
a = []
We use pass statement in Python to write empty loops. Pass
is also used for empty control statements, functions and # list with int values
classes.
a = [1, 2, 3]
for letter in 'geeksforgeeks':
print(a)
pass
# list with mixed int and string
print('Last Letter :', letter) o/p: Last Letter : s b = ["Geeks", "For", "Geeks", 4, 5]
data types print(b) o/p: [1, 2, 3]
1. Numeric Data Types in Python
['Geeks', 'For', 'Geeks', 4, 5]
The numeric data type in Python represents the data that
has a numeric value. A numeric value can be an integer, a Tuple Data Type
floating number, or even a complex number.
Just like a list, a tuple is also an ordered collection of Python
Integers – This value is represented by int class. It objects. The only difference between a tuple and a list is that
contains positive or negative whole numbers tuples are immutable i.e. tuples cannot be modified after it is
(without fractions or decimals). created. It is represented by a tuple class.
Float – This value is represented by the float class. It # initiate empty tuple
is a real number with a floating-point representation.
It is specified by a decimal point. t1 = ()
s1 = set()
s1 = set("GeeksForGeeks")
o/p: Set with the use of String: {'e', 'G', 's', 'F', 'o', 'r', 'k'}
d = {}
print(d)
width: Width of the button in letters Python provides various options for developing graphical
user interfaces (GUIs). The most important features are
wraplength: If this value is set to a positive number, listed below.
the text lines will be wrapped to fit within this
length. Tkinter − Tkinter is the Python interface to the Tk
GUI toolkit shipped with Python. We would look at
import tkinter as tk this option in this chapter.
def button_clicked(): wxPython − This is an open-source Python interface
print("Button clicked!") for wxWidgets GUI toolkit. You can find a complete
tutorial on WxPython here.
root = tk.Tk()
PyQt − This is also a Python interface for a popular
# Creating a button with specified options cross-platform Qt GUI library. TutorialsPoint has a
very good tutorial on PyQt5 here.
button = tk.Button(root,
PyGTK − PyGTK is a set of wrappers written in Python
text="Click Me",
and C for GTK + GUI library. The complete PyGTK
command=button_clicked, tutorial is available here.
In OOPs, it makes it easy to maintain and modify < In procedural programming, the main program is divided
existing code as new objects are created inheriting into small parts based on the functions and is treated as
characteristics from existing ones. separate program for individual smaller program.
<
OOPs due to modularity in its programs is less
complex and hence new data objects can be
Complexity C, BASIC, COBOL, Pascal, etc. are the examples POP languages.
created easily from existing objects making
object-oriented programs easy to modify
Create a hierarchy of classes where subclasses Explain the purpose, parameters, return values, and
inherit attributes and methods from parent classes. any exceptions raised.
5. SOLID Principles:
substring_start = str[:5]