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

Chapter 2 Python

Python

Uploaded by

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

Chapter 2 Python

Python

Uploaded by

Mushtaq Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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