Python Basics
Python Basics
Content
• Comments in Python
• Variable Declaration in Python
• Data Types in Python
• Type Conversion in Python
• Operators in Python
Comments in Python
• In general, Comments are used in a programming language to
describe the program or to hide the some part of code from
the interpreter.
Numbers:
• Number stores numeric values. Python creates Number type
variable when a number is assigned to a variable.
There are three numeric types in Python:
1. int
2. float
3. Complex
Data Types in Python Cont..
1. int: Example:
Int, or integer, is a whole number, positive or a=10
negative, without decimals, of unlimited b=-12
length. c=123456789
2. float: Example:
Float or "floating point number" is a X=1.0
number, positive or negative, containing Y=12.3
one or more decimals. Z=-13.4
3. complex: Example:
Complex numbers are written with a "j" as A=2+5j
the imaginary part. B=-3+4j
C=-6j
Data Types in Python Cont..
String:
• The string can be defined as the sequence of characters
represented in the quotation marks. In python, we can use
single, double, or triple quotes to define a string.
Example:
S1=‘Welcome’ #using single quotes
S2=“To” #using double quotes
S3=‘’’Python’’’ #using triple quotes
Data Types in Python Cont..
Example: “datatypesdemo.py”
a=10
b=“Python"
c = 10.5
d=2.14j
print("Data type of Variable a :",type(a))
print("Data type of Variable b :",type(b))
print("Data type of Variable c :",type(c))
print("Data type of Variable d :",type(d))
Output:
python3 datatypesdemo.py
Datatype of Variable a : <class ‘int’>
Datatype of Variable b : <class ‘str’>
Datatype of Variable c : <class ‘float’>
Datatype of Variable d : <class ‘complex’>
Type Conversion in Python
• Python provides Explicit type conversion functions to directly
convert one data type to another. It is also called as Type Casting in
Python
• Python supports following functions
1. int () : This function converts any data type to integer.
2. float() : This function is used to convert any data type to a floating point
number.
3. str() : This function is used to convert any data type to a string.
Example:“Typeconversiondemo.py” Output:
x = int(2.8) python3 typeconversiondemo.py
y = int("3") 2
z = float(2) 3
s = str(10) 2
print(x);print(y) 10
print(z); print(s)
Operators in Python
• The operator can be defined as a symbol which is responsible for
a particular operation between two operands.
• Python provides a variety of operators described as follows.
Arithmetic operators :
+ (addition) eg: a=20; b=10 then a + b=30
- (subtraction) eg: a=20; b=10 then a - b=10
*(multiplication) eg: a=20; b=10 then a * b=200
/ (divide) eg: a=20; b=10 then a / b=2
%( reminder) eg: a=20; b=10 then a % b=0
// (floor division) eg: a=24; b=7 then a // b=3
** (exponent) eg: a=2; b=3 then a ** b=8
Membership operators :
in (True, If the value is present in the data structure)
not in (True, If the value is not present in the data structure)
Operators in Python Cont…