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

Lecture 2

The document covers basic data types in Python, including booleans, integers, floats, and strings, along with type conversions and variable declaration. It explains operations on numbers and strings, including concatenation, indexing, and various string methods. The session also highlights the Python interpreter environment and the concept of reference semantics in variable assignment.

Uploaded by

The Kalam House
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)
5 views14 pages

Lecture 2

The document covers basic data types in Python, including booleans, integers, floats, and strings, along with type conversions and variable declaration. It explains operations on numbers and strings, including concatenation, indexing, and various string methods. The session also highlights the Python interpreter environment and the concept of reference semantics in variable assignment.

Uploaded by

The Kalam House
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

Data and Information

Management with Python


Session-2

©2023 Grant Thornton India LLP. All rights reserved.


Agenda
1. Basic data types bool (Boolean), int (Integer/Long), float, complex
2. Type conversions: into to float, float to int, etc.
3. Python Interpreter and its Environment
4. Numbers
5. Strings
6. Declaration of variables
7. Basic Operations in Python
8. String definition and manipulation commands

2 ©2023 Grant Thornton India LLP. All rights reserved.


Simple Data Types
Simple Data Types

• Numbers
– Integer, floating-point, complex!

• Strings
– characters are strings of length 1

• Booleans are False or True

4 ©2023 Grant Thornton India LLP. All rights reserved.


Numbers

• The usual notations and operators


 12, 3.14, 0xFF, 0377, (-1+2)*3/4**5, abs(x), 0<x<=5
• C-style shifting & masking
 1<<16, x&0xff, x|1, ~x, x^y
• Integer division truncates :-(
 1/2 -> 0 # float(1)/2 -> 0.5
• Long (arbitrary precision), complex
 2L**100 -> 1267650600228229401496703205376L
 1j**2 -> (-1+0j)

5 ©2023 Grant Thornton India LLP. All rights reserved.


Strings and Formatting

i = 10

d = 3.1415926

s = "I am a string!"

print "%d\t%f\t%s" % (i, d, s)

print “newline\n"

print "no newline"

6 ©2023 Grant Thornton India LLP. All rights reserved.


Variables

• No need to declare
• Need to assign (initialize)
 use of uninitialized variable raises exception
• Not typed
if friendly: greeting = "hello world"
else: greeting = 12**2
print greeting
• Everything is a variable:
 functions, modules, classes

7 ©2023 Grant Thornton India LLP. All rights reserved.


Reference Semantics

• Assignment manipulates references


 x = y does not make a copy of y
 x = y makes x reference the object y
references
• Very useful; but beware!
• Example:
>>> a = [1, 2, 3]; b = a
>>> [Link](4); print b
[1, 2, 3, 4]

8 ©2023 Grant Thornton India LLP. All rights reserved.


Simple Data Types: Operators

• + - * / % (like C)
• += -= etc. (no ++ or --)
• Assignment using =
– but semantics are different!
a=1
a = "foo" # OK
• Can also use + to concatenate strings

9 ©2023 Grant Thornton India LLP. All rights reserved.


Strings

• "hello"+"world" "helloworld" # concatenation


• "hello"*3 "hellohellohello" # repetition
• "hello"[0] "h" # indexing
• "hello"[-1] "o" # (from end)
• "hello"[1:4] "ell" # slicing
• len("hello") 5 # size
• "hello" < "jello" 1 # comparison
• "e" in "hello" 1 # search
• New line: "escapes: \n "
• Line continuation: triple quotes ’’’
• Quotes: ‘single quotes’, "raw strings"

1
©2023 Grant Thornton India LLP. All rights reserved.
0
Simple Data Types

• Triple quotes useful for multi-line strings


>>> s = """ a long
... string with "quotes" or anything else"""
>>> s
' a long\012string with "quotes" or anything else'
>>> len(s)
45

1
©2023 Grant Thornton India LLP. All rights reserved.
1
Methods in String

• upper() • strip(), lstrip(), rstrip()

• lower() • replace(a, b)

• capitalize() • expandtabs()

• count(s) • split()

• find(s) • join()

• rfind(s) • center(), ljust(), rjust()

• index(s)

1
©2023 Grant Thornton India LLP. All rights reserved.
2
Practical
What we have learned?

• Basic data types bool (Boolean), int (Integer/Long),


float,complex
• Type conversions: into to float, float to int, etc.
• Python Interpreter and its Environment
• Numbers
• Strings
• Declaration of variables
• Basic Operations in Python
• String definition and manipulation commands

14 ©2023 Grant Thornton India LLP. All rights reserved.

You might also like