Variable and Data Type
Variable and Data Type
in/
1
Comments :
Comments in Python are identified with a hash symbol, #, and extend to the end of the
line. Hash characters in a string are not considered comments, however. There are
three ways to write a comment - as a separate line, beside the corresponding statement
of code, or as a multi-line comment block.
There are multiple uses of writing comments in Python. Some significant uses include:
n
●
●
Increasing readability
e.i
Explaining the code to others
lif
● Understanding the code easily after a long-term
● Including resources
on
Single-Line Comments:
Single-line comments begin with the “#” character. Anything that is written in a single
line after ‘#’ is considered as a comment. The syntax for writing single-line comments is:
# comments here
There are two ways of using single-line comments in Python. You can use it before the
code or next to the code. The example depicted below shows the use of comments in
both ways.
Website:https://pythonlife.in/
2
Data Types:
Data types are the classification or categorization of data items. It represents the kind of
value that tells what operations can be performed on a particular data. Since everything
is an object in Python programming, data types are actually classes and variables are
instance (object) of these classes.
n
e.i
lif
on
th
Py
Strings: Strings in Python are used to store textual information. They are used to carry out operations
that perform positional ordering among items.
Lists: The list data type is the most generic Python data type. Lists can consist of a collection of mixed
data types, stored by relative positions.
Website:https://pythonlife.in/
3
Tuples: Python Tuples are one among the immutable Python data types that can store values of mixed
data types. They are basically a list that cannot be changed.
Sets: Sets in Python are a data type that can be considered as an unordered collection of data without
any duplicate items.
Dictionaries: Dictionaries in Python can store multiple objects, but unlike lists, in dictionaries, the objects
are stored by keys and not by positions.
Python Keywords :
n
Python keywords are reserved words. They are used by python interpreters to understand the program.
e.i
Keywords define the structure of programs. We can’t use keywords to name program entities such as
variables, classes, and functions.
lif
on
th
as except is True
def global or
del if pass
Identifier :
● Identifier is a name used to identify a variable, function, class, module, etc. The identifier is a combination of
character digits and underscore.
● The identifier should start with a character or Underscore then use a digit.
● The characters are A-Z or a-z, an Underscore ( _ ) , and digit (0-9).
● We should not use special characters ( #, @, $, %, ! ) in identifiers.
• var1
• _var1
• _1_var
• var_1
n
Examples for invalid identifiers:
•
!var1
1var
e.i
lif
• 1_var
on
• var#1
th
Py
Variable:
● Python Variable is containers which store values.
● Python is not “statically typed”.
● We do not need to declare variables before using them or declare their type.
● A variable is created the moment we first assign a value to it.
● A Python variable is a name given to a memory location.
● It is the basic unit of storage in a program.
Website:https://pythonlife.in/
5
n
e.i
lif
on
th
Py
Website:https://pythonlife.in/
6
Variable Cases:
There are 3 types of variable cases in python:
1.Camel Case
2.Snake Case
3.Pascal Case
n
e.i
lif
on
th
Py
Camel Case:
● Suppose we have a list of words, we have to concatenate them in camel case
format.
● So, if the input is like ["Hello", "World", "Python", "Programming"], then the output
will be "helloWorldPythonProgramming“.
Snake Case:
Snake case (stylized as snake_case) refers to the style of writing in which each space is
replaced by an underscore (_) character, and the first letter of each word is written in
lowercase.
It is a commonly used naming convention in computing, for example for variable and
subroutine names, and for filenames.
One study has found that readers can recognize snake case values more quickly than
camel case.
n
Pascal Case: e.i
lif
Pascal case follows the same camel case naming convention rules — all but one: we
capitalize the first letter.
on
In object-oriented languages like Java and TypeScript, we use pascal case to denote
classes, namespaces, and abstractions like interfaces.
th
In scripting languages like Python, you'll rarely find camel or pascal case code except
Py
int() -
● This function is used to convert any data type into an integer data type.
● The syntax of int() is int(variable, base), the function takes 2 parameters.
● where “variable” is a string and ‘base’ specifies the base in which string is if the
data type is a string.
float() –
Website:https://pythonlife.in/
8
● This function is used to convert any data type into a float type.
● The syntax of float() is float(parameter), where parameter is optional.
● The use of float without parameters is only to declare an empty float data type
variable.
complex()-
● The complex() function is used to create a complex number or convert a string or number to a
complex number.
● The complex type is described in Numeric Types — int, float, complex.
str() –
● Used to convert an integer into a string.
● This function is mostly used when we need to combine different data type values with string data
type values.
n
● Syntax of str() function is str(parameter).
Input():
e.i
lif
● In Python, we use input() function to take input from the user.
● Whatever you enter as input, the input function converts it into a string.
on
● If you enter an integer value still input() function convert it into a string.
Syntax:
th
input(prompt)
Py
Parameter:
Return:
String object