0% found this document useful (0 votes)
4 views14 pages

Python Chapter 2 Notes

Python BCA mod 2 notes

Uploaded by

keerthysalesh03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Python Chapter 2 Notes

Python BCA mod 2 notes

Uploaded by

keerthysalesh03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 2: Data Types, Input and Output in Python

2.1 Data Types

What is a Data Type?

A data type specifies the kind of value a variable can store.

Think of it like containers:

Container Stores

int Whole numbers

float Decimal numbers

str Text

bool True/False

list Multiple values

tuple Fixed collection

set Unique values

dict Key-value pairs

Example:

a = 10

b = 3.14

c = "Python"

• a → integer

• b → float

• c → string

Python Data Types Classification

Python Data Types

├── Numbers

│ ├── Integer

│ ├── Float
│ └── Complex

├── Boolean

├── None

├── Sequence Types

│ ├── String

│ ├── List

│ └── Tuple

└── Unordered Types

├── Set

└── Dictionary

Number Data Types

1. Integer (int)

Stores whole numbers.

Examples:

a = 10

b = -25

c=0

Output:

print(type(a))

<class 'int'>

2. Float

Stores decimal values.

Examples:

x = 3.14

y = 10.5
Output:

print(type(x))

<class 'float'>

3. Complex Numbers

Contains real and imaginary parts.

Format:

a+bj

Example:

z = 2+3j

Here:

• Real part = 2

• Imaginary part = 3j

Output:

print(type(z))

<class 'complex'>

Boolean Data Type

Stores only:

True

False

Example:

is_student = True

Output:

print(type(is_student))

<class 'bool'>

Important

Correct:

True

False

Wrong:
true

false

Python is case-sensitive.

None Data Type

Represents absence of value.

Example:

x = None

Output:

print(type(x))

<class 'NoneType'>

Use case:

name = None

Means name has no value yet.

Sequence Types

Sequence means data stored in order.

String

Collection of characters.

Examples:

name = "Gautham"

city = 'Kochi'

Python has no separate character type.

'a'

is a string of length 1.

String Example

str1 = "apple"

str2 = "mango"

Output:
print(str1)

apple

List

Ordered collection.

Written inside:

[]

Example:

list1 = [1,2,3,4]

Mixed data allowed:

list2 = [1,20.8,"apple"]

Why List?

Can store many values in one variable.

marks = [80,85,90,95]

List is Mutable

Can be changed.

numbers = [1,2,3]

numbers[0] = 100

print(numbers)

Output:

[100,2,3]

Tuple

Ordered collection like list.

Written using:

()

Example:

t = (1,2,3)
Tuple is Immutable

Cannot be changed.

t=(1,2,3)

t[0]=100

Error!

Difference Between List and Tuple

List Tuple

[] ()

Mutable Immutable

Can modify Cannot modify

Unordered Types

No fixed position.

Set

Stores unique values.

Written inside:

{}

Example:

s = {1,2,3,4}

Duplicate values removed.

s = {1,1,2,2,3}

print(s)

Output:

{1,2,3}
Properties of Set

✔ No duplicates

✔ Unordered

✔ Mutable

Example:

fruits = {"apple","orange","mango"}

Dictionary

Stores data in Key:Value format.

Example:

student = {

"name":"John",

"age":20

Output:

print(student["name"])

John

Why Dictionary?

Real-world records.

employee = {

"id":101,

"name":"Gautham",

"salary":50000

2.2 Comments in Python

Comments explain code.

Python ignores comments.

Single-line comment:

# This is a comment
Example:

# Display Hello

print("Hello")

Output:

Hello

Multi-line Comments

Method 1:

# Line 1

# Line 2

# Line 3

Method 2:

"""

Line 1

Line 2

Line 3

"""

Why Comments?

1. Improve readability

2. Documentation

3. Easier maintenance

2.3 Indentation in Python

This is VERY IMPORTANT.

Unlike C, Java, C++ which use {}

Python uses spaces.

Example:

if True:

print("Correct")
The space before print is indentation.

Wrong:

if True:

print("Correct")

Output:

IndentationError

Rule

Use 4 spaces.

if age > 18:

print("Adult")

else:

print("Minor")

2.4 Multi-Line Statements

Long statements can be broken into multiple lines.

Using backslash:

total = first + \

second + \

third

Without backslash inside brackets:

months = [

"January",

"February",

"March"

Python understands automatically.

2.5 Multiple Statement Group (Suite)


A suite is a block of statements.

Example:

if mark > 50:

print("Pass")

print("Congratulations")

Both statements belong to the if block.

2.6 Quotes in Python

Python supports:

Single Quote

name = 'John'

Double Quote

name = "John"

Triple Quote

message = """

This is line 1

This is line 2

"""

Used for:

• Multi-line strings

• Documentation

2.7 Displaying Output

Python uses:

print()

Example:

print("Hello")

Output:

Hello
Printing Variables

a = 10

print(a)

Output:

10

Multiple Values

print(1,2,3,4)

Output:

1234

sep Parameter

Changes separator.

print(1,2,3,4,sep="+")

Output:

1+2+3+4

end Parameter

Changes ending character.

print("Hello",end="%")

Output:

Hello%

type() Function

Returns datatype.

Example:

a = 10

b = "Python"
print(type(a))

print(type(b))

Output:

<class 'int'>

<class 'str'>

2.8 Reading Input

Python uses:

input()

Example:

name = input("Enter your name:")

print(name)

Important

input() always returns string.

age = input("Enter age:")

Even if user enters 20.

Age becomes:

"20"

(string)

To convert:

age = int(input("Enter age:"))

2.9 Import Function

When programs become large, code is organized into modules.

To use module:

import math

Example:

import math
print([Link])

Output:

3.141592653589793

Other Examples

import random

import datetime

2.10 Type Conversion

Converting one datatype to another.

int()

Convert to integer.

x = int("10")

Result:

10

float()

x = float("10")

Result:

10.0

str()

x = str(100)

Result:

"100"

list()

x = list("ABC")

Output:
['A','B','C']

tuple()

x = tuple([1,2,3])

Output:

(1,2,3)

set()

x = set([1,1,2,3])

Output:

{1,2,3}

You might also like