Chapter 2 Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Chapter 2: Variables and Data types

A variable is the name given to a memory location


in a program. For example:
 A = 30
 b = "Harry"
 c = 71.22
Data Types
Primarily, there are the following data types in
Python:
 Integers
 Floating point numbers
 Strings
 Booleans
 None
Python is a fantastic language that automatically
identifies the type of data for us:
a = 71 ⇒ Identifies a as class <int>
b = 88.44 ⇒ Identifies b as class <float>

name = "Harry" ⇒ Identifies name as


class <str>
Rules for defining a Variable name:
 A variable name can contain alphabets, digits,
and underscores.
 A variable name can only start with an
alphabet and underscore.
 A variable name can't start with a digit.
 No white space is allowed to be used inside a
variable name.
Examples of a few variable names are:
harry, one8, seven, Seven, etc.
Operators in Python
Following are some common operators in Python:

1.Arithmetic operators ⇒ +, -, *, /, etc.


2.Assignment operators ⇒ =, +=, -=, etc.

3.Comparison operators ⇒ ==, >, <, >=, <=, !


=, etc.
4.Logical operators ⇒ and, or, not

type() function and Typecasting


The type function is used to find the data type of a
given variable in Python.
type(a) ⇒ class <int>
a = 31

type(b) ⇒ class <str>


b = "31"

A number can be converted into a string and vice


versa (if possible).
There are many functions to convert one data type
into another:
str(31) ⇒ "31" ⇒ Integer to string conversion
int("32") ⇒ 32 ⇒ String to integer conversion
float(32) ⇒ 32.0 ⇒ Integer to float conversion
... and so on

input() function
input() function in programming. It describes how
this function allows users to take input from the
keyboard as a string.
Key points include:
1.The syntax: a = input("Enter name").
 If the user enters "harry", then a will be

"harry".
 If the user enters "34", will still be a string
"34".
2.It emphasizes that the output of the input()
function is always a string, even if a number is
entered.
e "31" is a string literal and 31 is a numeric literal
Chapter 2 - Practice Set
1.Write a Python program to add two numbers.

2.Write a Python program to find the remainder


when a number is divided by 2.

3.Check the type of the variable assigned using


the input() function.
4.Use comparison operators to find out whether
a given variable a is greater than b or not.
Take a = 34 and b = 80.

5.Write a Python program to find the average of


two numbers entered by the user.

6.Write a Python program to calculate the square


of a number entered by the user.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy