0% found this document useful (0 votes)
11 views30 pages

4a. From Algorithms To Python

The document serves as an introduction to Python programming, explaining fundamental concepts such as programming languages, program execution, and the structure of Python code. It covers the basics of writing and executing Python programs, including the use of print statements, strings, escape sequences, and comments. Additionally, it provides guidance on creating and organizing Python files, along with practical exercises for learners to apply their knowledge.

Uploaded by

Farih Nour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views30 pages

4a. From Algorithms To Python

The document serves as an introduction to Python programming, explaining fundamental concepts such as programming languages, program execution, and the structure of Python code. It covers the basics of writing and executing Python programs, including the use of print statements, strings, escape sequences, and comments. Additionally, it provides guidance on creating and organizing Python files, along with practical exercises for learners to apply their knowledge.

Uploaded by

Farih Nour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

From Algorithms to

Building Python Programs


Part 1: Introduction to Python & Python Basics
It has often been said that a person does not really understand
something until after teaching it to someone else.
Actually a person does not really understand something until after
teaching it to a computer, i.e., expressing it as an algorithm.

 Source: Knuth, Don. Selected Papers on Computer Science. Stanford. CA:


Center for the Study of Language and Information, 1996
Programming
 program: A set of instructions
to be carried out by a computer.
• code = a program fragment, some lines of a program
• to code = to program

 program execution: The act of carrying out the instructions


contained in a program.
• To execute a program = to run a program

 programming language: A systematic set of rules used to describe


computations in a format that is editable by humans.
Some modern languages
 procedural languages: programs are a series of commands
• Pascal (1970): designed for education
• C (1972): low-level operating systems and device drivers

 functional programming: functions map inputs to outputs


• Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990)

 object-oriented languages: programs use interacting "objects"


• Smalltalk (1980): first major object-oriented language
• C++ (1985): "object-oriented" improvements to C
⁻ successful in industry; used to build major OSes such as Windows
• Python (1991):
⁻ It has aspects of all of the above types of programming languages!
Why Python?
 Relatively simple

 Pre-written software

 Widely used
How will we approach Python?
 Similarly to flowcharts
 You have already written algorithms in flowcharts
 You will see many of the same things again, written differently
 And some new ideas
Python: compiled or
interpreted?
 Compilation: source code is translated into binary machine code
completely before it can be executed
• C is a compiled language
• A compiler performs the translation

 Interpretation: source code is translated into binary machine code a


bit at a time (incrementally), so you can write and execute your code
piece by piece
• Python is an interpreted language
• An interpreter translates and executes your program step by step
Let’s begin with output
This is how the program communicates with the outside world
A Python program
print("Hello, world!")
print()
print("This program produces")
print("four lines of output")

 Its output:
Hello, world!

This program produces


four lines of output

 console: Text box into which


the program's output is printed.
print
 A statement that prints a line of output on the console.

 Two ways to use print (for the time being):


• print("text")
Prints the given message as output.

• print()
Prints a blank line of output.
Strings and escape
sequences
Strings
 string: A sequence of characters to be printed.
• Starts and ends with a " quote " character or a ' quote ' character.
⁻ The quotes do not appear in the output.

• Examples:
"hello"
"This is a string. It's very long!"
'Here is "another" with quotes in'
"""I can span multiple lines
because I'm surrounded by 3 quotes"""
 strings are text values
Strings

 Restrictions:
• Strings surrounded by " " or ' ' may not span multiple lines
"This is not
a legal String."

• Strings surrounded by " " may not contain a " character.


"This is not a "legal" String either."

• Strings surrounded by ' ' may not contain a ' character.


'This is not a 'legal' String either.'
Escape sequences

 escape sequence: A special sequence of characters used to represent certain


special characters in a string.
\t tab character
\n new line character \ is the escape character
\" quotation mark character
\' quotation mark character
\\ backslash character

• Example:
print("\\hello\nhow\tare \"you\"?\\\\")

• Output:
\hello
how are "you"?\\
Questions (1)
 What is the output of the following print statements?
print("\ta\tb\tc")
print("\\\\")
print("'")
print("\"\"\"")
print("C:\nin\the downward spiral")

 Write a print statement to produce this output:


/ \ // \\ /// \\\
Answers (1)
 Output of each print statement:
a b c
\\
'
"""
C:
in he downward spiral

 print statement to produce the line of output:


print("/ \\ // \\\\ /// \\\\\\")
Questions (2)

 What print statements will generate this output?


This quote is from
Irish poet Oscar Wilde:

"Music makes one feel so romantic


- at least it always gets on one's nerves –
which is the same thing nowadays."
Answers (2)

 print statements to generate the output:


print("This quote is from")
print("Irish poet Oscar Wilde:”)
print()
print("\"Music makes one feel so romantic")
print("- at least it always gets on one's nerves -")
print("which is the same thing nowadays.\"")
Questions (3)
 What print statements will generate this output?
A "quoted" String is
'much' better if you learn
the rules of "escape sequences."

Also, "" represents an empty String.


Don't forget: use \" instead of " !
'' is not the same as "
Answers (3)
 print statements to generate the output:
print("A \"quoted\" String is")
print("'much' better if you learn")
print("the rules of \"escape sequences.\"")
print()
print("Also, \"\" represents an empty String.")
print("Don't forget: use \\\" instead of \" !")
print("'' is not the same as \"")
Creating a Python Program
Creating a Python Program File
 In Visual Studio Code:
• File menu
• New file
• Python file
Creating a Python Program File
(2)

When you run it you get:


Running a Python Program File
(3)
Anatomy of a Filename (1)
Example:
D:\Courses\CSC 1401\Programs\helloworld.py
extension

filename

path
Anatomy of a Filename (2)
 The filename says what is in the file.
• Give it a meaningful name!
• The extension says what type of file it is. E.g.,
⁻ .py : a Python program
⁻ .c : a C program
⁻ .txt : a plain text file
⁻ .mp3 : an audio file
 The path says where it is located in a directory
structure that is organized as a tree hierarchy.
Commenting your
program
Comments
 comment: A note written in source code by the programmer to
describe or clarify the code.
• Comments are not executed when your program runs.

 Syntax:
# comment text

 Examples:
# This is a one-line comment.
# This is a very long
# multi-line comment.
Comments example
# Suzy Student,
# CSc 110, Fall 2019
# Displays lyrics

# first line
print("When I first got into magic")
print("it was an underground phenomenon")
print()

# second line
print("Now everybody's like")
print("pick a card, any card")
Activity 22 – Print out info about
yourself
Write a Python program that prints out the following information in exactly the format
below. I use a fictional student as an example. You can substitute in your information.

Hi! I’m Happy Tobehere


I am 20 years old<TAB>oops, 21
I like the "Rolling Stones" and 'Ultravox’

I know how to use ", ', and \ in Python.


Goodbye!

Use a single print statement for the last two lines.


Submit to Exercise 13 in Canvas.

You might also like