INTRO   O

PROBLEM SOLVING AND

PROGRAMMING IN PYTHON

 

(use the Space key to navigate through all slides)

Prof. Andrea Gallegati
tuj81353@temple.edu

Prof. Dario Abbondanza
tuk96119@temple.edu

CIS 1051 - INTRO PYTHON

python

Its developers want it to be fun to use.

The name was a tribute to the British comedy group Monty Python.

@andreagalle ➜ /workspaces/CIS1051-python (master) $ python
Python 3.12.1 (main, Dec 12 2024, 22:30:56) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\n Hi guys! ¯\\_(ツ)_/¯ \n')  # this is a comment, on the left there is a function

 Hi guys! ¯\_(ツ)_/¯ 

>>> 

What Is a Program?

if you had a press release like this

The President of the United States,

Donald Trump,
has been meeting with Congress and Senate
to discuss plans for the economy.
Wall Street and taxes are among the main topics
of discussion.

The President of the United States,

Donald Trump,
has been meeting with Congress and Senate 
to discuss plans for the economy.
Wall Street and taxes are among the main topics
of discussion.

just applying the above code ...

import re

with open("doc/document.txt", "r") as file:
    text = file.read()

    print("\n" + text + "\n")
    
replacements = {
    "President": "Supreme Leader",
    "United States": "The Banana Republic",
    "Donald Trump": "Barack Obama",
    "Congress": "The Honorable Council of Elders",
    "Senate": "The High Council of Wise Ones",
    "House of Representatives": "The Assembly of the People",
    "economy": "banana economy",
    "Wall Street": "Banana Street",
    "taxes": "banana tariffs",
    "stock market": "banana market"
}
for old, new in replacements.items():
    text = re.sub(old, new, text)

with open("doc/funny_document.txt", "w") as file:
    file.write(text)

print("\n ... and the replacement is complete! \n")
print("\n" + text + "\n")

... watch out for fake news!

The Supreme Leader of the The Banana Republic, Barack Obama,
has been meeting with
The Honorable Council of Elders and The High Council of Wise Ones to discuss plans for the banana economy.

Banana Street and banana tariffs are among the main topics of discussion.

The President of the United States,

Donald Trump,
has been meeting with Congress and Senate
to discuss plans for the economy.
Wall Street and taxes are among the main topics
of discussion.

What Is a Program?

  • or something graphical, like processing an image
from PIL import Image
from IPython.display import display

with Image.open("img/lgbt-logo.jpg") as im:
    display(im.convert("L"))  # opts: L, 1, RGB, "P", palette=Image.ADAPTIVE, colors=256

What Is a Program?

  • or something graphical, like processing an image
  • even playing a video backwards !
from PIL import Image, ImageSequence
from IPython.display import display

im = Image.open("img/monty-python.gif")
frames = [frame.copy() for frame in ImageSequence.Iterator(im)]
frames.reverse()
frames[0].save('img/reverse-monty-python.gif', save_all=True, 
               append_images=frames[1:], loop=0)

DETAILS LOOK DIFFERENT, BUT

  • Input:

    Get data from the keyboard, a file, the network

  • Output:

    Send data on the screen, to a file, over the network

  • Math:

    Basic math operations (+/-*)

  • Conditional execution:

    Check for certain conditions and run the appropriate code

  • Repetition:

    Perform some action repeatedly, usually with a little variation

no matter how complicated

think of programming as the process of
breaking a large, complex task into smaller 
and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions.

We will use programming as a means to that end!

How to think like a computer scientist

  • Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations).
     
  • Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives.
     
  • Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.

most important skill is problem solving

The ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately.

Formal and Natural Languages

Natural languages

languages people speak.

  • English
  • Spanish
  • French

 

Not designed by people, they evolved naturally.

Formal languages

designed by people for specific applications.

  • mathematical notation is good at denoting relationships among numbers and symbols
     
  • structural formula of
    chemicals is a graphic representation of the molecular structure

Programming languages

 are formal languages that have been designed to express computations.

ambiguity:

Natural languages are full of ambiguity, which people deal with by using contextual clues and other information.

Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context.

 

redundancy:

As a result, they are often verbose. Formal languages are less redundant and more concise

In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy.

literalness:

Formal languages, simply

mean exactly what they say.

Natural languages are full of idiom and metaphor ...

is like the difference between poetry and prose

  • poetry

    Words are used for their sounds, meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common but often deliberate.

  • prose

    The literal meaning of words is more important, and the structure contributes more meaning. 

  • programs

    The meaning of a computer program is unambiguous and literal, and can be understood entirely its analysis.

This was crafted with

A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations

Made with Slides.com