0% found this document useful (0 votes)
22 views8 pages

Python Unit-1 Part Ii

The document discusses various data types and type conversion functions in Python. It describes the different built-in data types like integers, floats, strings, lists, tuples, dictionaries and sets. It also explains type conversion functions like int(), float(), str(), list(), tuple() and dict() to convert between data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Python Unit-1 Part Ii

The document discusses various data types and type conversion functions in Python. It describes the different built-in data types like integers, floats, strings, lists, tuples, dictionaries and sets. It also explains type conversion functions like int(), float(), str(), list(), tuple() and dict() to convert between data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Unit-1 Part- Introduction to

Programming 2 Python

Comments in python:
• Including comments in programs makes code more readable for humans as it
provides some information or explanation about what each part of a program is
doing.
• Depending on the purpose of your program, comments can serve as notes to
yourself or reminders, or they can be written with the intention of other
programmers being able to understand what your code is doing.
• In general, it is a good idea to write comments while you are writing or updating
a program as it is easy to forget your thought process later on, and comments
written later may be less useful in the long term.
• Comments in Python begin with a hash mark (#) and whitespace character and
continue to the end of the line.

# This is a comment

• In a “Hello, World!” program, a comment may look like this:

# Print “Hello, World!” to console


print("Hello, World!")

• Inline comments occur on the same line of a statement, following the code itself.
Like other comments, they begin with a hash mark and a single whitespace
character.
• Generally, inline comments look like this:

[code] # Inline comment about the code

• Example:
z = 2.5 + 3j # Create a complex number

• Multiline comments in python can be placed in between triple single quotes ‘’’ or triple
double quotes “””
• Example:

1
Python Unit-1 Part- Introduction to
Programming 2 Python

Data types in python:


Python has following standard Data Types:
dat
Description Nature Exampl
a
e
typ
e
i=25

Integer, is a whole j=234456778800


number, positive or type(i)#<class ‘int’>
Immutabl
negative, without
e type(j)#<class ‘int’>
decimals, of
int
unlimited length. (un- print(i) #25
changeabl
e) print(j) # 234456778800

“Floating point
f=23.456
number" is a number,
positive or negative, type(f) #<class ‘float’>
float containing one or Immutabl
e print(f)
more decimals.

c1=20+50.25j
Complex numbers
type(c1) #<class ‘complex’>
Form: x+yj
print(c1) #20+50.25j
x: real part
complex Immutabl print(c1.real)# 20
y: imaginary part e
print(c1.imag) # 50.25

Strings: combination
of characters. s1=’hello’
Each character in s2=”HELLO”
string can be
accessed by index. s3=’’’hello python’’’
Immutabl
e type(s1)# <class ‘str’>
Positive index
str starts from ‘0’(left print(s2) # HELLO
to right).
print(s3[2])# l
Negative index
starts from’-1’(right
to left).
b1=True
b2=False
bool Boolean: True or Immutabl
False e type(b1)#<class ‘bool’>
print(b2)# False

2
Python Unit-1 Part- Introduction to
Programming 2 Python

List is a sequence of lst=[1,2,3,4,5]


ordered type(lst)# <class ‘list’>
homogeneous
list elements. Symbol: [ Mutable print(lst)# [1,2,3,4,5]
] print(lst[2])# 3
Supports indexing print(lst[-2])# 4
(positive,
negative).

3
Python Unit-1 Part- Introduction to
Programming 2 Python

tuple is a sequence
of ordered tup=(10,’hello’,True)
heterogeneous
objects. type(tup)# <class ‘tuple’>
Immutabl
e print(tup[0]) #10
tuple Symbol: ( )
print(tup[-2]) #hello
Supports indexing
(positive,
negative).
Set is a collection of vowels={‘a’,’e’,’i’,’o’,’u’}
un- ordered type(vowels)# <class ‘set’>
elements. Mutable
set print(vowels)
Symbol: { }
# {‘a’,’e’,’i’,’o’,’u’}
Discards the
duplicates.
d={1:’a’,2:’b’,3:’c’}
Dictionaries are used
for mapping Mutable type(d) # <class ‘dict’>
dict (key,value) pairs.
print(d) #{1:’a’,2:’b’,3:’c’}

************************************************************
Type Conversion functions:
• Python defines type conversion functions to directly convert one data type to
another which is useful in day to day and competitive programming.
• Following are some conversion functions.
int(a, base) : This function converts any data type to integer. ‘Base’ specifies the
base in which string is if data type is string.
float() : This function is used to convert any data type to a floating point number

Program:
.
s = "10010"
c = int(s,2)
print ("After converting to integer base 2 : ",c)
e = float(s)
print ("After converting to float : ",e)

Output:
After converting to integer base 2 : 18
After converting to float : 10010.0

ord() : This function is used to convert a character to


integer. hex() : This function is to convert integer to
hexadecimal string. oct() : This function is to convert
4
Python Unit-1 Part- Introduction to
Programming 2 Python
integer to octal string.

5
Python Unit-1 Part- Introduction to
Programming 2 Python

Program:

s = '4'
c = ord(s)
print ("After converting character to integer : ",c)
print (c)
d = hex(56)
print ("After converting 56 to hexadecimal string :
",d)
e = oct(56)
print ("After converting 56 to octal string : ",e)
Output:

After converting character to integer : 52


After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70

tuple() : This function is used to convert to a tuple.


set() : This function returns the type after converting to set.
list() : This function is used to convert any data type to a list type.

s = 'geeks'
t = tuple(s)
print ("After converting string to tuple : ",t)
st = set(s)
print ("After converting string to set : ",st)
l = list(s)
print ("After converting string to list : ",l)
Output:

After converting string to tuple : ('g', 'e', 'e', 'k', 's')


After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']

dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
str() : Used to convert integer into a string.
complex(real,imag) : : This function converts real numbers to complex(real,imag)
number.
# Python code to demonstrate Type conversion
# using dict(), complex(), str()

# initializing integers
a =1
b =2

# initializing tuple
tup = (('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to complex number
c = complex(1,2)
print ("After converting integer to complex number : ",end="")
print (c)

6
Python Unit-1 Part- Introduction to
Programming 2 Python

# printing integer converting to string


c = str(a)
print ("After converting integer to string : ",end="")
print (c)

# printing tuple converting to expression dictionary


c = dict(tup)
print ("After converting tuple to dictionary : ",end="")
print (c)
Output:

After converting integer to complex number : (1+2j)

After converting integer to string : 1

After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

Simple Input and Output functions:


• input() and print() are widely used for standard input and output operations
respectively.
• We use the print() function to output data to the standard output device (screen).

• In the second print() statement, we can notice that a space was added
between the string and the value of variable a.This is by default, but we can
change it.
• The actual syntax of the print() function is

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

• Here, objects is the value(s) to be printed.


• The sep separator is used between the values. It defaults into a space character.
• After all values are printed, end is printed. It defaults into a new line.
• The file is the object where the values are printed and its default
value is sys.stdout(screen). Here are an example to illustrate this.

• In Python, we have the input() function to allow this. The syntax for input() is

7
Python Unit-1 Part- Introduction to
Programming 2 Python

• where prompt is the string we wish to display on the screen. It is optional.

• Here, we can see that the entered value 10 is a string, not a number. To
convert this into a number we can use int() or float() functions.

You might also like