Download Computer Science 1MD3 Introduction to Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Winter 2014
Computer Science 1MD3
Introduction to Programming
Michael Liut ([email protected])
Brandon Da Silva ([email protected])
Ming Quan Fu ([email protected])
S
www.michaelliut.ca/cs1md3
Introduction
S Computer Science Students
S Easily Approachable
S Office Hours Held By Graduate TA & Professor – TBA
S E-mail Us!
www.michaelliut.ca/cs1md3
Class Resources
S http://www.cas.mcmaster.ca/~franek/courses/cs1md3/
S All announcements, assignments, marks, and course
material will be posted here.
www.michaelliut.ca/cs1md3
Setting up Python
S Download the installation file from the web
S http://www.python.org
S Click on “Download” on the left
S Download the Python 3.3.3 installation file
S Make sure you are selecting the right file as there are different
files for different Operating Systems.
S There is a difference between 32- and 64-bit operating systems
S Need Help?
http://www.cas.mcmaster.ca/~franek/courses/cs1md3/hel
p/help.cgi
www.michaelliut.ca/cs1md3
Using IDLE
S To open up IDLE, click…
S Start > All Programs > Python 3.3 > IDLE (Python GUI)
S To create a new program, click…
S File > New Window
S To edit a program, click…
S File > Open
www.michaelliut.ca/cs1md3
Text Editors
S You may use any you wish. Python has its own text editor.
S Others:
S Notepad++
S TextWrangler
S Sublime Text
www.michaelliut.ca/cs1md3
Who uses Python?
S
Python is known as a programming language for everyone, as beginners, professionals,
and even computer scientists use it for programming
S
Beginners enjoy Python’s simplicity, and particularly like its ‘batteries included’
appearance, meaning that with one simple download, one has everything they need for
basic programming without having to import or download more add-ons.
S
Professionals and computer scientists often use Python as an alternative to Perl, for its
scripting and string manipulation capabilities, yet its syntax is not as cryptic as other
languages
S
Python has been used by companies like IBM, Disney, NASA, Google, Dropbox,
Industrial Light and Magic, and Zope Corporation.
www.michaelliut.ca/cs1md3
How easy is it to learn?
S Very easy to learn and experiment with
S Interactive Environment
S Compatible with many platforms
S Designed for beginners and advanced programmers
S One of the easiest languages to learn
S Python documentation
S http://docs.python.org/3.3/tutorial/index.html
www.michaelliut.ca/cs1md3
Learning to Program
S
www.michaelliut.ca
/cs1md3
The Beginning
S Two main ways to program in Python, Interactive Mode
Programming or Script Mode Programming
S We use Script Mode Programming
S Basics:
S Commenting  # in Python instead of // in Java
S New line in text  \n
S Tab  \t
www.michaelliut.ca/cs1md3
Variables
Name
Example
String (holds numbers and text)
stringVariable = “Hello world!”
Boolean (True or False value)
booleanVariable = True
*Note: True and False must have the
first letter capitalized
Integer (a positive or negative whole
number)
intVariable = 123
Floating-Point Numbers (a positive
or negative number with a decimal)
floatVariable = 1.0
Imaginary Numbers (square root of 1, represented by a j)
imaginaryVariable = 12j
www.michaelliut.ca/cs1md3
Naming Conventions
General Naming
S Use meaningful names
S Use words existing in the terminology of the target domain
S AVOID EXCESSIVELY LONG NAMES
S Use names relative to what you are assigning
www.michaelliut.ca/cs1md3
Naming Conventions (ctd)
Variable Names
S Single word variable names are all lower case
S Example: “age”, “gender”, etc.
S Multiple word variable names capitalize the first letter of the
SECOND word in the variable
S Example: “firstName”, “lastName”, “pointScored”
www.michaelliut.ca/cs1md3
Naming Conventions (ctd)
Constants
S USE ALL CAPS
S Example: “WIDTH”, “LENGTH”, etc.
S Multiple words are separated but underscores “_”
S Example: “SCREEN_SIZE”, “MAX_AGE”
www.michaelliut.ca/cs1md3
Declaring Variables
firstName is a type string
age is a type integer
firstName = “Bob”
age = 17
www.michaelliut.ca/cs1md3
Printing Text
S As mentioned earlier, Python is known for its “batteries
included” appearance, meaning you do not have to import
to print text
S Example:
Printing a string
print(“Hello World!”)
>> Hello World!
www.michaelliut.ca/cs1md3
Text and Variables
S There are two basic ways to display variables when outputting text
in Python
S Examples:
Printing
print(“Here is an example with text and variables: %s %d %f ” %(“ABC”,
123, 3.14))
>> Here is an example with text and variables: ABC 123 3.14
print(“My name is ” + firstName + “, I am ” + str(age) + “ years old”)
>> My name is Bob, I am 17 years old
www.michaelliut.ca/cs1md3
Common String Functions
S There are a lot of string functions! Here are a few:
text = “Hello World!”
print(text.count(“l”))
>> 3
Count how many of a letter or word is
in the text
print(text[6:11])
>>World
Extract substrings (reminder that the
first letter of the text is 0, like in
arrays)
print(text.lower()) print(text.upper())
Change to lower/upper case
>> hello world!
>> HELLO WORLD!
print(text.replace("Hello", "Goodbye"))
>> Goodbye World!
Replace the first world in the brackets,
with the second word in the brackets
www.michaelliut.ca/cs1md3
Print Functions
S print(“Here is an example with text and variables: %s %d
%f ” %(“ABC”, 123, 3.14))
S The percent signs represent the type of variable being
outputted – MUST MATCH
S print(“Make a new line\n”)
S “\n” at the end of text creates a new line
www.michaelliut.ca/cs1md3
Keyboard Input
S In Python, the input function reads anything that the user
inputs as a string
Example
Keyboard Input
number = input(“Insert a number: ”)
www.michaelliut.ca/cs1md3
If Statements
If Statement
if number == 1:
print(“The number is 1”)
elif number > 1:
print(“The number is ” + number)
else:
print(“Please choose a different number”)
www.michaelliut.ca/cs1md3
While Loop
Python 3 Example:
counter = 0
go = True
while go == True:
if counter == 2:
go = False
print(“Gone”)
else:
counter += 1
print(“Going…”)
www.michaelliut.ca/cs1md3
For Loop
S In Python, you use something called the for… in… loop
S In Python it is set up as:
S
for <variable name> in range (<start number>, <end
number>, <step>):
S Example
Python
for i in range (10, 0, -1):
print("T-minus: " + str(i))
www.michaelliut.ca/cs1md3
Working with Lists in Python
grades = [89, 76, 92, 83]
grades.append(94)
print(grades)
>>[89, 76, 92, 83, 94]
Appending a number to a list adds it
to the end of the list.
grades.insert(2, 67)
print(grades)
>>[89, 76, 67, 92, 83]
The first number in the brackets when
inserting a number tells Python
where in the list you want to put it.
Like in Java, the list starts at 0. All
the other variables after it get pushed
to the right one.
print(grades[3])
>> 83
Like in Java, Python can call certain
variables from the list
www.michaelliut.ca/cs1md3
Assignment 1
DUE: THURSDAY JANUARY 30, 2014
BY 11PM
www.michaelliut.ca/cs1md3
The End
S
www.michaelliut.ca/cs1md3