Python Unit-1 Part Ii
Python Unit-1 Part Ii
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
• 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:
• 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
“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
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
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:
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:
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
• 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
• 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
• 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.