Lab Task # 3 Introduction To Programming With Python
Lab Task # 3 Introduction To Programming With Python
Python Strings
String literals in python are surrounded by either single quotation marks, or double quotation marks.
Example 1.
Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1])
Example 2.
b = "Hello, World!"
print(b[2:5])
Example 3.
The strip() method removes any whitespace from the beginning or the end:
Example 4.
a = "Hello, World!"
print(len(a))
Example 5.
a = "Hello, World!"
print(a.lower())
Example 6.
a = "Hello, World!"
print(a.upper())
Example 7.
a = "Hello, World!"
print(a.replace("H", "J"))
Example 8.
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
The following example asks for the user's name, then, by using the input () method, the program
prints the name to the screen:
Example 1.
x = input()
print("Hello, " + x)
Typecasting
Int( )
Float( )
Str( )
Access Items
Example 1.
print(thislist[1]).
Task # 1: Create a List of ten string elements using Python and print them on screen.
Example 1.
thislist[1] = "blackcurrant"
print(thislist)
You can loop through the list items by using a for loop:
Example 1.
for x in thislist:
print(x)
Task # 2: Create a list of 10 string elements and print them using for loop.
Check if Item Exists
Example 1.
if "apple" in thislist:
List Length
To determine how many items a list has, use the len() method:
Example 1.
print(len(thislist))
Add Items
To add an item to the end of the list, use the append() method:
Example 1.
thislist.append("orange")
print(thislist)
To add an item at the specified index, use the insert() method:
Example 1.
thislist.insert(1, "orange")
print(thislist)
Remove Item
Example 1.
thislist.remove("banana")
print(thislist)
del thislist
thislist.clear()
print(thislist)
Example
print(thislist)
List Methods
Python has a set of built-in methods that you can use on lists.
Method Description
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
If statement:
a = 33
b = 200
if b > a:
Indentation
Python relies on indentation, using whitespace, to define scope in the code. Other programming
languages often use curly-brackets for this purpose.
Example 1.
a = 33
b = 200
if b > a:
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this
condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding condition
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Task # 03: Create a program of taking three inputs from user and compare them and print
which one is greater.
Task # 04: Create a program of taking five subjects marks using for loop and print
Percentage and Grade of Students.
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
Example
If you have only one statement to execute, one for if, and one for else, you can put it all on the same
line:
Example
print("A") if a > b else print("B")
You can also have multiple else statements on the same line:
The and keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b, AND if c is greater than a:
if a > b and c > a:
print("Both conditions are True")
Or
Python Functions
Creating a Function
def my_function():
print("Hello from a function")
Calling a Function
def my_function():
print("Hello from a function")
my_function()
Parameters
Example 1
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Return Values
To let a function return a value, use the return statement
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Arrays
Example 1:
Example
x = cars[0]
cars[0] = "Toyota"
Use the len() method to return the length of an array (the number of elements in an array).
Example
x = len(cars)
You can use the for in loop to loop through all the elements of an array.
Example
Print each item in the cars array:
for x in cars:
print(x)
Adding Array Elements
Task # 06: Create a program which takes 10 values and store them in to an array A and 10
values in array B and store their addition in array C and print on screen.
Python Classes/Objects
Create a Class
class MyClass:
x=5
Create Object
Example
p1 = MyClass()
print(p1.x)