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.
• Comments in Python can be used to explain any program
code. It can also be used to hide the code as well.
• Comment is not a part of the program, but it enhances the
interactivity of the program and makes the program readable.
Python supports two types of comments:
• Single Line Comment
• Multi Line Comment
Comments in Python Cont..
Single Line Comment:
In case user wants to specify a single line comment, then comment must start
with ‘#’
Example:
# This is single line comment
print "Hello Python"
Output:
Hello Python
Multi Line Comment:
Multi lined comment can be given inside triple quotes.
Example:
'''This is
Multiline
Comment'''
print "Hello Python"
Output:
Hello Python
Variable Declaration in Python
• A variable is a named memory location in which we can store
values for the particular program.
• In other words, Variable is a name which is used to refer
memory location. Variable also known as identifier and used
to hold value.
• In Python, We don't need to declare explicitly variable in
Python. When we assign any value to the variable that
variable is declared automatically.
• In Python, We don't need to specify the type of variable
because Python is a loosely typed language.
Variable Declaration in Python Cont..
• In loosely typed language no need to specify the type of
variable because the variable automatically changes it's data
type based on assigned value.
Rules for naming variable:
• Variable names can be a group of both letters and digits, but
they have to begin with a letter or an underscore.
• It is recommended to use lowercase letters for variable name.
‘SUM’ and ‘sum’ both are two different variables.
Example: Vardemo.py
a=10 #integer output:
b="StudyGlance" #string $python3 Vardemo.py
c=12.5 #float 10
print(a) StudyGlance
print(b) 12.5
print(c)
Variable Declaration in Python Cont..
• Python allows us to assign a value to multiple variables and
multiple values to multiple variables in a single statement
which is also known as multiple assignment.
• Assign single value to multiple variables :
Example: Vardemo1.py output:
x=y=z=50 $python3 Vardemo1.py
print x 50
print y 50
print z 50
• Assign multiple values to multiple variables :
Example: Vardemo2.py output:
a,b,c=5,10,15 $python3 Vardemo2.py
print a 5
print b 10
print c 15
Data Types in Python
• In general, Data Types specifies what type of data will be
stored in variables. Variables can hold values of different data
types.
• Python is a dynamically typed or loosely typed language,
hence we need not define the type of the variable while
declaring it.
• The interpreter implicitly binds the value with its type.
• Python provides us the type () function which enables us to
check the type of the variable.
Data Types in Python Cont..
• Python provides following standard data types, those are
Numbers
String
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.
• In the case of string handling, the operator + is used to
concatenate two strings as the operation "hello"+"
python" returns "hello python".
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…
Comparison operators : Bitwise operators :
== (Equal to) & (binary and)
!= (Not equal to) | (binary or)
<= (Less than or equal) ^ (binary xor)
>= (Greater than or equal) ~ (negation)
< (Less than) << (left shift)
> (Greater than) >> (right shift)
Assignment operators : Logical operators :
= (Assigns to) and (logical and)
+= (Assignment after Addition) or (logical or)
-= (Assignment after Subtraction) not (logical not)
*= (Assignment after Multiplication)
Identity operators :
/= (Assignment after Division)
is (Returns true if both variables are
%= (Assignment after Modulus)
the same object)
**= (Assignment after Exponent)
//= (Assignment after floor division)
is not (Returns true if both variables
are not the same object)