Python 1
Python 1
1
◼
◦ COBOL
◦ business data
◦ LISP
◦ logic and AI
◦ BASIC
◦ a simple language
11
◼
Sequence, Selection, and Iteration are the basic
elements that we use to tell the computer what to
do.
variable_name = value
X = 10
◼ Examples:
print ("Hello, world!“)
age = 37
print (“Your age is: “,age)
Output:
Hello, world!
Your age is 37
◼ USSAMA YAQUB, DISC 325 17
◼
Number manipulation
Example:
x = 14 + 16
print(x)
Y = X / 2
print(y)
Output:
for i in range(100):
print(i)
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
◦ It can also accept a third value specifying the change between values.
◦ range(start, stop, step) - the integers between start (inclusive)
and stop (exclusive) by step
◦ Example:
for x in range(5, 0, -1):
print (x)
print ("Blastoff!”)
Output:
5
4
3
2
1
Blastoff!
*
**
***
****
*****
sum = 0
for i in range(1, 11):
sum = sum + i
Output:
?
while condition:
# code to execute
o Offer more flexibility than for loop as because the condition can be
based on any logical expression.
prog_stat = “"
while prog_stat != “Exit":
age = int(input("How old are you? "))
print ("Your age is", age)
print ("You have", 60 - age, "years until retirement")
prog_stat = (input("Enter Exit to stop"))
Example:
gpa = 3.4
if gpa > 2.0:
print ("Your application is
accepted.“)
if condition:
statements
elif condition:
statements
else:
statements
◦ Syntax:
if condition:
statements
else:
statements
Example:
gpa = 1.4
if gpa > 2.0:
print "Welcome to The
University!"
else:
print "Your application is
denied."
Examples:
5%2 = 1 (5 = 2*2 + 1)
10%4 = 2 (10 = 4*2 + 2)
20%10 = 0 (20 = 10*2 + 0)
25%13 = 12 (25 = 13*1 + 12)
count = 0
for i in range(5):
num = int(input('Enter a number: '))
if num>10:
count=count+1
print('There are', count, 'numbers greater than 10.')
count = 0
for i in range(1,101):
if (i**2)%10==4:
count = count + 1
print(count)
sum = 0
for i in range(1,101):
sum = sum + i
print('The sum is', sum)
44
◼
Strings
string: A sequence of text characters in a program.
◦ Strings start and end with quotation mark " or apostrophe ' characters.
◦ Examples:
“Hello"
"This is a string"
"This, too, is a string. It can be very long!"
A string may span across multiple lines or contain a " character. A triple-quote is
used for this purpose:
""" This is
a legal String. ""“
index 0 1 2 3 4 5 6 7 8 9
character a b c d e f g h i j
◦ Example:
print(s, "starts with", s[0])
Output:
abcdefghij starts with a
index 0 1 2 3 4 5 6 7 8 9
character a b c d e f g h i j
◦ Example:
print(s[-1],s[-2])
Output:
j i
◦ Example:
s = “abcdefghij"
◦ for x in range(len(s)):
print(s[x])
◦ If we don’t want to keep track of location in the string, we can use a simpler
loop:
◦ for x in s:
print(x)
◦ Example:
s = “abcdefghij"
Example:
name = "Martin Douglas Stepp"
length = len(name)
big_name = str.upper(name)
print(big_name, "has", length, "characters“)
Output:
MARTIN DOUGLAS STEPP has 20 characters
◼ USSAMA YAQUB, DISC 325 ◼52
Lab Assignment
Write a program that asks the user to enter a string and prints out the
number of vowels in the string. Also print the position of these vowels in
the string. Print any punctuation in the string such as ?,!. Also count the
number of parenthesis and whether their order is correctly contained in
the input string.
Following is a sample user input string:
What is going (on) here! Is this the {Python [101]} class?
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40]
Data types Lists can contain all kinds of things, even other lists. For example,
the following is a valid list:
◦ Example:
L = [1, 2, 3, 4]
◦ for x in range(len(L)):
print(L[x])
◦ for item in L:
print(item)
Function Description
findall Returns a list containing all matches
Returns a Match object if there is a
search
match anywhere in the string
Returns a list where the string has
split
been split at each match
Replaces one or many matches with a
sub
string
import re
text = "I like to eat apples."
x = re.findall("[a-m]", text)
print(x)
import re
text = "That will be 59 dollars"
x = re.findall("\d", text)
print(x)
◼ USSAMA YAQUB, DISC 325 66
◼
Regular expressions
# Replace part of a string:
text = "I like to eat apples."
new_text = re.sub(pattern, "banana", text)
print(new_text)
# Split a string:
text = "red, green, blue"
parts = re.split(",", text)
print(parts)