Python Lesson 2
Lesson # 2
Hi All,
Comments
Comments are very useful and important in any programming
language. In python we also use comments to make our code more readable and
definable. But remember
‘Code never lies, but comments lies’ –
So, make your comments as per code so it exactly defines why
we write a comment and what we need to explain at that time.
Because programmer works with different languages and on
different projects. So, write your comment in that way if you read after 1 year
you remember or understand what’s the logic behind and it helps you to figure
out why I write this section of code etc.
Syntax of comments
è
In python # symbol used for
comment
è
For multiline you can use #
symbol or “”” (three times double quotation)
Example
# single line comment
“””
Comment 1
Comment 2
“””
So, between “”” and “”” is
comments in python
Variables
As I told in lesson 1 that in python no need to put the data
type for variables in python.
Below are few variables
MyString = “Line 1” -> String
MyNumber = 10 -> Integer
But the rules for writing variable names are
è
Should start with letter or
underscore
è
Cannot start with number
i.e. 9MyString (this is wrong)
è
Variable contains alpha
numeric characters and underscores only
è
Variables are case
sensitive
So, be careful with variable
names. My suggestion is always use letter for creating the name of variable and
always make meaningful variable names
Datatypes in Python
There are 7 types of datatypes that are built-in and are
below:
1.
Text -> str
2.
Numeric -> int, float,
complex
3.
Sequence -> list, tuple,
range
4.
Mapping -> dict
5.
Set -> set, frozenset
6.
Boolean -> bool
7.
Binary -> bytes,
bytearray, memoryview
You can use method type() to know the datatype of variable
Example
MyString = “Line 1”
Print (“MyString have ” +
type(MyString) + “ datatype”)
Complex datatype
In Numeric type the complex is little bit different, for
complex we need to add ‘j’ at the end of value, so python interpreter
understands this is complex numeric datatype.
Example
MyComplexValue = 10j
Casting
Sometimes we need type casting from one datatype to another
Example
MyString = “10”
But we need to add 20 in MyString but MyString is a string
so we cannot add the 20
MyCastVariable = int(MyString)
-> This will return 10 as integer
See you in lesson 3 :-)
Comments
Post a Comment