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

Postlab 4223

This document provides examples of string, tuple, list, and arithmetic operations in Python. It demonstrates how to concatenate and slice strings, unpack and index tuples, append to and slice lists, perform arithmetic on mixed data types, and use comparison and conditional operators. Loops such as while, for, and continue are also illustrated along with break and else clauses. Finally, it shows how to perform type conversions between int, float, complex, and string data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Postlab 4223

This document provides examples of string, tuple, list, and arithmetic operations in Python. It demonstrates how to concatenate and slice strings, unpack and index tuples, append to and slice lists, perform arithmetic on mixed data types, and use comparison and conditional operators. Loops such as while, for, and continue are also illustrated along with break and else clauses. Finally, it shows how to perform type conversions between int, float, complex, and string data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NUMERICAL COMPUTING

ZERO LAB
22K-4223

STRINGS
A string is a sequence of characters enclosed in single or double quotes. Strings are concatenated with the
plus (+) operator, whereas slicing (:) is used to extract a portion of the string.
#1

string1 = 'Welcome to learn Python'


string2 = 'for Numerical Computing'
print(string1 + ' ' + string2) # Concatenation
print(string2[4:23]) # Slicing
OUTPUT;

#2

#A string can be split into its component parts using the split command. The
components appear as elements in a list. For example,
s = '3 9 81'
print(s.split()) # Delimiter is white space
OUTPUT;

#3

#A string is an immutable object—its individual characters cannot be modified with


an assignment statement,
#and it has a fixed length. An attempt to violate immutabilitywill result in
TypeError, as follows:
s = 'Press return to exit'
s[0] = 'p'
correct code the above will give error
s = 'Press return to exit'
s = 'p' + s[1:]
print(s)
OUTPUT;

VARIABLES
A tuple is a sequence of arbitrary objects separated by commas and enclosed in parentheses. If the tuple contains a
single object, a final comma is required; for example, x = (2,). Tuples support the same operations as strings; they are
also immutable. Here is an example where the tuple rec contains another tuple (6,23,68):
#1

b = 2
# b is integer type
print(b)
b = b*2.0 # Now b is float type
print(b)
OUTPUT;

TUPLES
#1

rec = ('Smith','John',(6,23,68))
# This is a tuple
lastName,firstName,birthdate = rec # Unpacking the tuple
print(firstName)
birthYear = birthdate[2]
print(birthYear)
name = rec[1] + ' ' + rec[0]
print(name)
print(rec[0:2])
OUTPUT;
LISTS;
A list is similar to a tuple, but it is mutable, so that its elements and length can be changed. A list is identified by
enclosing it in brackets. Here is a sampling of operations that can be performed on lists:
#1

a = [1.0, 2.0, 3.0] # Create a list


a.append(4.0) # Append 4.0 to list
print(a)
(0,0.0) # Insert 0.0 in position 0
print(a)
print(len(a)) # Determine length of list
a[2:4] = [1.0, 1.0, 1.0] # Modify selected elements
print(a)
OUTPUT;

#2

#Matrices can be represented as nested lists, with each row being an element of the
list. Here is a 3×3 matrix a in the form of a list:
a = [[1, 2, 3],\
[4, 5, 6], \
[7, 8, 9]]
print(a[1]) # Print second row (element 1)
print(a[1][2]) # Print third element of second row
OUTPUT;
Arithmetic Operators
Python supports the usual arithmetic operators:
Arithmetic Operations
Addition +
Subtraction −
Multiplication ∗
Division /
Exponentiation ∗∗
Modular division %

#1

s = 'Hello'
t = 'to you'
a = [1, 2, 3]
print(3*s) # Repetition
print(3*a) # Repetition
[1, 2, 3, 1, 2, 3, 1, 2, 3]
print(a + [4, 5]) # Append elements
print(s + t) # Concatenation
print(3 + s) # This addition makes no sense

THE ABOVE WONT WORK CORRECTED CODE IS BELOW

s = 'Hello'
t = 'to you'
a = [1, 2, 3]
# Repetition with strings and lists
print(3 * s) # HelloHelloHello
print(3 * a) # [1, 2, 3, 1, 2, 3, 1, 2, 3]
# Append elements to a list
print(a + [4, 5]) # [1, 2, 3, 4, 5]
# Concatenation of strings
print(s + t) # Helloto you
# Adding a string to an integer after converting integer to string
print(str(3) + s) # 3Hello
OUTPUT;
Python also has augmented assignment operators, such as a + = b, that are familiar to the users of C. The augmented
operators and the equivalent arithmetic expressions are shown in following table.
Augmented Assignment Operator
a += b a=a+b
a -= b a=a-b
a *= b a = a*b
a /= b a = a/b
a **= b a = a**b
a %= b a = a%b

The comparison (relational) operators return True or False. These operators are
Comparison Operators
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

#1

a = 2 # Integer
b = 1.99 # Floating point
c = '2' # String
print(a > b)
print(a == c)
print((a > b) and (a != c))
print((a > b) or (a == b))
OUTPUT;
CONDITIONAL STATEMENTS;
#1

def sign_of_a(a):

if a < 0.0:
sign = 'negative'
elif a > 0.0:
sign = 'positive'
else:
sign = 'zero'
return sign

a = 1.5
print( 'a is ' + sign_of_a(a))
OUTPUT;

LOOPS;
The while construct while condition: block executes a block of (indented) statements if the condition is True. After
execution of the block, the condition is evaluated again. If it is still True, theblock is executed again. This process is
continued until the condition becomes False.Theelse clause else: block can be used to define the block of statements
that are to be executed if the condition is false.
#1

nMax = 5
n =1
a = [] # Create empty list
while n < nMax:
a.append(1.0/n) # Append element to list
n=n+1
print(a)
OUTPUT;

#2

nMax = 5
a ==[]
for n in range(1,nMax):
a.append(1.0/n)
print(a)
#Here n is the target, and the range object [1, 2, ..., nMax1](created by calling
the range function) is the sequence.
#Any loop can be terminated by the break statement.
ABOVE CODE IS WRONG BELOW IS CORRECTED
nMax = 5
a = [] # Use '=' for assignment
for n in range(1, nMax):
a.append(1.0 / n)
print(a)
OUTPUT;

#3

list = ['Jack', 'Jill', 'Tim', 'Dave']


name = eval(input('Type a name:')) #python input prompt
for i in range(len(list)):
if list[i] == name:
print(name,'is number',i + 1,'on the list')
break
else:
print(name,'is not on the list')
# While running code don't forget to give 'Tim' rather then Tim
OUTPUT;

#4

x = [] # Create an empty list


for i in range(1,100):
if i%7 != 0: continue # If not divisible by 7, skip rest of loop
x.append(i) # Append i to the list
print(x)
OUTPUT;

If an arithmetic operation involves numbers of mixed types, the numbers are automatically converted to a common
type before the operation is carried out. Type conversions can also achieved by the following functions:
Type Conversion
int(a) Converts a to integer
float(a) Converts a to floating point
complex(a) Converts to complex a+0j
complex(a,b) Converts to complex a+bj

#1

a = 5
b = -3.6
d = '4.0'
print(a + b)
1.4
print(int(b))
OUTPUT;

#2

print(complex(a,b))

print(float(d))

print(int(d)) # This fails: d is a string

print(int(d))
print(float(d))
OUTPUT;

You might also like