Python Unit-1 Part Ii-1
Python Unit-1 Part Ii-1
# 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:
2
Unit-1 Part- 2 Introduction to Python
“Floating point
f=23.456
number" is a number,
positive or negative, type(f) #<class ‘float’>
float containing one or Immutable
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 Immutable print(c1.real)# 20
y: imaginary part print(c1.imag) # 50.25
Strings: combination of
characters. s1=’hello’
Each character in string s2=”HELLO”
can be accessed by
s3=’’’hello python’’’
index. Immutable
type(s1)# <class ‘str’>
Positive index starts
str from ‘0’(left to right). print(s2) # HELLO
print(s3[2])# l
Negative index starts
from’-1’(right to left).
b1=True
b2=False
bool Boolean: True or False Immutable
type(b1)#<class ‘bool’>
print(b2)# False
List is a sequence of lst=[1,2,3,4,5]
ordered homogeneous type(lst)# <class ‘list’>
elements. Symbol: [ ]
list Mutable print(lst)# [1,2,3,4,5]
Supports indexing print(lst[2])# 3
(positive, negative). print(lst[-2])# 4
3
Unit-1 Part- 2 Introduction to Python
tuple is a sequence of
ordered heterogeneous tup=(10,’hello’,True)
objects.
type(tup)# <class ‘tuple’>
Symbol: ( ) Immutable
print(tup[0]) #10
tuple Supports indexing print(tup[-2]) #hello
(positive, negative).
************************************************************
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
Program:
a=10
print(a)
print(type(a))
b=float(a)
print(b)
print(type(b))
output:
10
<class 'int'>
10.0
<class 'float'>
Program:
a=10.5
print(a)
print(type(a))
b=int(a)
print(b)
print(type(b))
output:
10.5
<class 'float'>
10
<class 'int'>
Program:
a='11'
print(a)
print(type(a))
b=int(a)
print(b)
print(type(b))
output:
11
5
Unit-1 Part- 2 Introduction to Python
<class 'str'>
11
<class 'int'>
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
6
Unit-1 Part- 2 Introduction to Python
7
Unit-1 Part- 2 Introduction to Python