Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

SENG 113: Computer Programming I

Doç.Dr. Hilal ARSLAN

Week 3: Introduction to Python

Week 3: Introduction to Python 1 / 32


Computer Programming
Outline

• Script Mode Modules

• The print and input statements

• Strings
The input Statement

The input statement takes input


values via the keyboard:

input(< string that serves as a prompt > )

Later we will learn how to input data from a file.


Temp and Wind via input
WindChill2.py
“““Computes windchill as a function of wind(mph)and
temp (Fahrenheit).”””
Temp = float(input(‘Enter Temp (Fahrenheit):’))
Wind = float(input(‘Enter wind speed (mph):’))

# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (WC)
Strings
Strings are quoted characters. Here are three
examples:

>>> s1 = ‘abc’
>>> s2 = ‘ABC’
>>> s3 = ‘ A B C ‘

s1, s2, and s3 are variables with string value.


Strings are Indexed

>>> s = ‘The Beatles’

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

The characters in a string can be referenced


through their indices. Called “subscripting”.

Subcripting from zero creates a disconnect: ‘T’ is not the first character.
Strings are Indexed

>>> s =‘The Beatles’


>>> t = s[4]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10
t --> B

0
The square bracket notation is used. Note, a single character is a string.
String Slicing

>>> s =‘The Beatles’


>>> t = s[4:8]

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

t --> B e a t

0 1 2 3
We say that “t is a slice of s”.
String Slicing

>>> s =‘The Beatles’


>>> t = s[4:]

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

t --> B e a t l e s

0 1 2 3 4 5 6
Same as s[4:11]. Handy notation when you want an “ending slice.”
String Slicing
>>> s =‘The Beatles’
>>> t = s[:4]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10

t --> T h e

0 1 2 3
Same as s[0:4]. Handy notation when you want a “beginning slice”.
String Slicing
>>> s =‘The Beatles’
>>> t = s[11]
IndexError: string index out of
range

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10
The is no s[11]. An illegal to access.
Subscripting errors are EXTREMELY common.
String Slicing
>>> s =‘The Beatles’
>>> t = s[8:20]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10

t --> l e s

0 1 2
It is “OK” to shoot beyond the end of the source string.
Strings Can Be Combined

>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1+s2

s --> T h e B e a t l e s

This is called concatenation.

Concatenation is the string analog of additionexcept


Concatenation

>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1 + ‘ ‘ + s2

s --> T h e B e a t l e s

We “added” in a blank.

No limit to the number of input strings: s = s2+s2+s2+s2+s2


Types

Strings are a type: str


So at this point we introduced 3 types:
int for integers, e.g., -12
float for decimals, e.g., 9.12, -12.0
str for strings, e.g., ‘abc’, ’12.0’

Python has other built-in types. And we will learn to make up our own types.
A Type is a Set of Values and
Operations on Them
Values…

int 123, -123, 0


float 1.0, -.00123, -12.3e-5
str ‘abcde’, ‘123.0’

These are called “literals”


The “e” notation (a power-of-10 notation) is handy for very large or very small
A Type is a Set of Values and
Operations on Them

Operations…

int + - * / ** %
float + - * / **
str +

concatenation
Question
Write a program that computes the result of
the given formula for three given integer (a, b
and c) numbers. You will input the numbers and
print out result of the calculation
Solution
Strings
• A string is a sequence of letters (called
characters).
• In Python, strings start and end with single or
double quotes.

>>> “foo”
‘foo’
>>> ‘foo’
‘foo’
Strings
Each string is stored in the computer’s
memory as a list of characters.
>>> myString = “GATTACA”
Question

Write a program which takes a string and


calculate how many ‘A’ are there in this string ?

• Convert upper form to this string


• Convert lower form to this string
• Check whether this string starts with ‘A’
• Replace all ‘A’ to ‘X

• Input: output:
Solution
Strings
Problem
Write a program that reads the first command
line argument as a DNA sequence and prints the
first three codons, one per line, in uppercase
letters.
Question
Write a program that reads a protein sequence
as a command line argument, convert upper
case, and prints the location of the first
cysteine residue.
Question
Write a program that reads a DNA sequence
as a string and print index of the last
occurence of ‘C’
Example
Write a string decoder
that takes a special kind
of string containing two
subscripts that has a
digit and a character
sequence. These two
substrings are
separated by a delimiter
character (|).
Homework
Write a python
script that
replaces the
last occurence
of the given
substring with
another
substring.

You might also like