0% found this document useful (0 votes)
12 views

Variable and Data Type

The document discusses different Python programming concepts like print statement, comments, data types, keywords, identifiers, variables and variable cases. It provides details and examples of single-line, multi-line and block comments. It also explains core data types in Python including numbers, strings, lists, tuples, sets and dictionaries.

Uploaded by

vfourprasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Variable and Data Type

The document discusses different Python programming concepts like print statement, comments, data types, keywords, identifiers, variables and variable cases. It provides details and examples of single-line, multi-line and block comments. It also explains core data types in Python including numbers, strings, lists, tuples, sets and dictionaries.

Uploaded by

vfourprasad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Website:https://pythonlife.

in/
1

The print statement:


● The print() function prints the given object to the standard output device (screen)
or to the text stream file.
● Remember the time when you wrote your first program? Yes! I am talking about
the “Hello World” program, which is probably the first program that anyone learns
in their life.

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

● Re-using the existing code


th
Py

Different Types of Comments:


There are three types of comments: single-line, multi-line. The syntax of comments
varies depending on the type. This tutorial will explore every kind of comment
individually, along with examples.

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.

Core Data Types in Python :


As the name suggests, a data type is the classification of the type of values that can be
assigned to variables. We have already seen that, here in Python, we don’t need to
declare a variable with explicitly mentioning the data type, but it’s still important to
understand the different types of values that can be assigned to variables in Python.
After all the data type of a variable is decided based on the value assigned.
in python:

n
e.i
lif
on
th
Py

Let’s discuss the above-mentioned core data types in Python.


Numbers: The number data type in Python is used to store numerical values. It is used to carry out
normal mathematical operations.

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

and else in return


Py

as except is True

assert finally lambda try

break false nonlocal with

class for None while

continue from not yield

def global or

del if pass

elif import raise


Website:https://pythonlife.in/
4

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.

Examples for valid 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

Rules for creating variables in Python:


● A variable name must start with a letter or the underscore character.
● A variable name cannot start with a number.
● A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ ).
● Variable names are case-sensitive (name, Name and NAME are three different
variables).
● The reserved words(keywords) cannot be used naming the variable.

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“.

ex: someVar, someClass, somePackage.xyz.


Website:https://pythonlife.in/
7

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.

ex: some_var, some_class, some_package.xyz.

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

when defining a class's name.

ex: SomeVar, SomeClass, SomePackage.xyz

Data conversion functions – int(), float(), complex(), str()

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:

Prompt: (optional) The string that is written to standard output (usually


screen) without newline.

Return:

String object

You might also like