Datatypes in Python 21 10 2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Comments in Python

In [ ]: --> Comments are the lines which we writing for understanding the code.

--> In Python If any line started with # Symbol then that particular line is a comment.

--> Comments will never be executed. Python will ignore all lines which are started with hash symbol.

--> It depends on the programmer weather he wants to use comments or not.

In [2]: a=10 #Value is 10


b=20 #Value is 20
print(a+b) #Addition of a and b

30

Single Line Comments in Python


In [ ]: --> If we want to create single line comment then we can make it with the help of # Hash Symbol.

In [ ]: #Addition of Two Number.


Result is Addition
a=10
b=20
print(a+b)

Multi Line Comments in Python


In [ ]: --> Multiline comments means we want to make comments of more than one line.

--> In Python we don't have any syntax for Multiline Comments in Python.

--> Many Developers are using Triple Quotation for Making Multiline Comments.

--> We can manually start all lines of the comments with # Symbol.

--> For Few IDE's There is one shortcut to make multiline comments with # Symbol manually --> ctrl+backslash

In [3]: '''Addition of Two Number.


Result is Addition'''
a=10
b=20
print(a+b)

30

In [ ]: #Addition of Two Number.


#Result is Addition
#Python is easy
#Java is hard
a=10
b=20
print(a+b)

In [ ]: # Addition of Two Number.


# Result is Addition
# Python is easy
# Java is hard
a=10
b=20
print(a+b)

Inline Comments in Python


In [ ]: --> You can add comments on the same line as code.

--> However, it's generally recommended to use these sparingly and only for very short, self-explanatory comments.

In [ ]: a=10 #Value is 10
b=20 #Value is 20
print(a+b) #Addition of a and b

Print() in Python
In [ ]: --> print function is used to display the content/output to the screen.

--> In Print Function we can print multiple values together.

In [5]: a=10
b=20
c=a+b
print(c)

30

In [6]: a=10
b=20
print(a,b)

10 20

Keywords/Reserved Words in Python


In [ ]: --> There are few words in Python whose meaning is already defined by Python and such kind of words are known as
keywords or Reserved Words or Predefined Words.

--> There are 35 Keywords in Python 3.12.1/Python 3.10.1 version.

Examples:

def --> Defining a function


True --> Boolean Value as 1
False --> Boolean Value as 0

In [9]: import keyword


print(keyword.kwlist)
print(len(keyword.kwlist))

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'i
n', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
35

Variables in Python
In [ ]: --> Variables are used to store data.

--> We are storing data inside a variable such that we can use that variable in future.

--> We can assign a variable name to a data/value.

In [15]: variable_name = 10.5


print(variable_name)

10.5

Rules for Defining a Variable Name/Function Name and Class Name


In [ ]: --> Variable name cannot be start with a digit.

--> Spaces are not allowed in between in the Variable name.

--> Variable name can only contain Alphabets(Upper or Lower) , Digits and Underscore(_).

--> Keywords cannot be used as a Variable Name.

In [16]: #Variable name cannot be start with a digit.

12total = 29
print(12total)

File "<ipython-input-16-b0ff1341c1ee>", line 3


12total = 29
^
SyntaxError: invalid decimal literal

In [17]: #Variable name cannot be start with a digit.


12_total = 23
print(12_total)

File "<ipython-input-17-f925172430b6>", line 2


12_total = 23
^
SyntaxError: invalid decimal literal

In [18]: #Spaces are not allowed in between in the Variable name.

student roll = 99

File "<ipython-input-18-9055642166c0>", line 3


student roll = 99
^
SyntaxError: invalid syntax

In [19]: #Variable name can only contain Alphabets(Upper or Lower) , Digits and Underscore(_).

ca$h = 10
print(ca$h)

File "<ipython-input-19-e1b2f716e7c7>", line 3


ca$h = 10
^
SyntaxError: invalid syntax

In [20]: #Variable name can only contain Alphabets(Upper or Lower) , Digits and Underscore(_).
ca_h = 10
print(ca_h)

10

In [22]: #Variable name can only contain Alphabets(Upper or Lower) , Digits and Underscore(_).
cah33 = 10
print(cah33)

10

In [21]: #Keywords cannot be used as a Variable Name.


False = 10

File "<ipython-input-21-892860a61661>", line 1


False = 10
^
SyntaxError: cannot assign to False

Pratice Question
In [ ]: # Which of the variable names are valid and which are invalid?

total99 = 200 #valid


student&roll = 99 #invalid
__name = 'Ansh' #valid
roll__ = 87 #valid
an@h = 90 #invalid
if = 50 #invalid
if_ = 60 #valid
defs = 95 #valid
student-name = 'Ansh' #invalid
student_name = 'Ansh' #valid

type() in Python
In [ ]: --> This is a function which is used to return the datatype of a variable.

In [23]: x = 10
print(type(x))

<class 'int'>

In [24]: x = 10.5
print(type(x))

<class 'float'>

In [25]: x = 'Pratyush'
print(type(x))

<class 'str'>

Datatypes in Python
In [ ]: Data+Types --> Which kind of data we are using in our Python Programming.

--> In Python Programming we need not to give datatype of the variable instead of that we can
directly assign the value to a variable.Python will automatically detect which kind of data type we
are using in our Program.

Example:

Mobile_num = Number/Integer

Name = String/Alphabet/Characters

Height = Decimal Number

Types of Datatypes in Python


In [ ]: Types of Datatypes in Python:

1. Numeric Datatype(Integer,Float Datatype)


2. Boolean Datatype
3. Strings
4. Data Structures(List,Tuple,Set and Dictionary)

Integer Datatypes in Python


In [ ]: --> Any number without decimal point is known as Integer Datatype

--> Whenever we are specifying any integer data then that number will never start 0

In [28]: num1 = 100


print(type(num1))
print(num1)

<class 'int'>
100

In [29]: num1 = -100


print(type(num1))
print(num1)

<class 'int'>
-100

In [31]: num1 = -7837464636346


print(type(num1))
print(num1)

<class 'int'>
-7837464636346

In [32]: num1 = 0
print(type(num1))
print(num1)

<class 'int'>
0

Float Datatype in Python


In [ ]: --> Any number with decimal point is known as Float datatype.

In [34]: num1 = 100.0


print(type(num1))
print(num1)

<class 'float'>
100.0

In [35]: num1 = -100.0


print(type(num1))
print(num1)

<class 'float'>
-100.0

In [36]: num1 = 0.0


print(type(num1))
print(num1)

<class 'float'>
0.0

In [37]: num1 = 123454.00002


print(type(num1))
print(num1)

<class 'float'>
123454.00002

Boolean Datatype
In [ ]: --> Boolean Datatypes are True or False.

--> True and False are Keywords in Python.

--> In Python Internally True means 1 and False means 0

In [38]: isMarried = True


print(type(isMarried))

<class 'bool'>

In [44]: is_graduated = False


print(type(is_graduated))

<class 'bool'>

In [41]: True+False+True+True
# 1 + 0 + 1 + 1

Out[41]: 3

In [42]: True*True*False
#1*1*0

Out[42]: 0

Input() Function
In [ ]: --> This Function is used to take input from the end user.

--> Input function will take everything in the form string.

In [46]: num1 = int(input('Enter a Number : '))


print(type(num1))
print(num1)

Enter a Number : 10
<class 'int'>
10

In [47]: num1 = float(input('Enter a Number : '))


print(type(num1))
print(num1)

Enter a Number : 19.8


<class 'float'>
19.8

Write a Python program to calculate your Body Mass Index (BMI).


In [51]: height = int(input())

mass = int(input())

bmi = mass/(height*height)
print(bmi)

6
65
1.8055555555555556

You might also like