acm init() spring 2015 day 1
TRANSCRIPT
Welcome!
Welcome to ACM init(), a Computer Science class for beginners
init() is a several week long class intended to introduce you to a language called Python
About init()• No prior programming
experience needed! In fact, it is assumed you have none
• Hosted by the Association for Computing Machinery (ACM)
• Meets once a week, 6-8pm. Days TBA
• You will learn how to code in a language called Python
Resources for you• ACM's website: uclaacm.com
• Slides posted at: slideshare.net/uclaacm
• ACM Facebook group: https://www.facebook.com/groups/uclaacm/
• init() Facebook group: https://www.facebook.com/groups/uclaacminit/
• My email: [email protected]
Why Computer Science?• Computer Science is a fast
growing field with a surplus of high-paying jobs
• Learning a programming language will enhance your resume and make you more desirable to companies (or grad schools!)
• Learning how to code will better your problem-solving skills and help you in all of your classes
Source: http://www.ovrdrv.com/blog/making-computer-science-cool/
TIOBE IndexThe chart on the previous page demonstrates the value of
Python. It is the 8th most popular programming language in the world right now!
!The TIOBE Index ranks the popularity of the top 100 programming languages based on number of job
advertisements.
Why learn Python?• Python is easy to read and
use, and is considered to be one of the best first languages to learn
• Python developers are in high demand
• Python is useful-- many different types of applications use Python
• Most important-- its logo is UCLA's colors!
Python demo- factorial• Factorial: written as #!
• For example, 5! is 5*4*3*2*1 = 120
• I wrote a simple program called fact.rb that will compute the factorial for us
• Let's try it out! Compute the factorial of 6.
A bit about computersComputers think in 0s and 1s. Everything that you input into
a computer gets stored in this manner. For example:
You don't need to know how this conversion works. However, the concept is important because it is the reason why we need programming languages.
Human-readable programming languages go through a process that converts them to bytes, which can be understood by the computer.
How do we make our code work?
We can't just type code into a word document and hope it will magically do something. We need a tool that will help
the computer understand our code.
Press enter, and the program will run!
That's all it takes! All Python programs will be run using python filename.py
Data types• There are three main data types in Python: numbers,
booleans, and strings.
• Numbers: Exactly what it sounds like. (example: 1, 400, 45.7)
• Booleans: Something that is either True or False (note the capitals).
• String: A word or phrase. (example: "Hello World!")
Data typesIt's important to know the difference between data types
because they are written and used differently.
5 = number "5" = string
!True = boolean "True" = string
VariablesWe can store these data types in variables so we can come
back and use them again later.
Take a look:
Now if we want to use any of these stored values again all we have to do is type the variable name!
VariablesSome things to notice:
!A number is just a regular number like 3 or 42.
!A boolean can only be True or False.
!A string must have quotes around it. Example: "This is my
string."
Variables
Looking at this example again, see that I called my variables my_num, my_string, etc. There are several naming
conventions to observe with variables. !
• Only start a variable with a lowercase letter. • No spaces in variable names. For multiple words, use '_'. • Don't use weird symbols like # or @. • Ok: this_is_a_variable = 3 • Bad: @@hi guys! = 3
Quick topic: printingWe'll often want to print to the computer screen. The command
for this is print
More about printingWhat if we try this?
Surprise- it won't work. We can fix it!
To access a variable within a string, use a %s inside the " " and put the variable name as % (variable_name) after.
String pitfalls
Make sure to always use double quotes " " !
If you use single quotes ' ' for strings it will work, but then if you ever try to use a contraction things will go wrong!
String methodsPython has some special built-in functions that we can use for
strings. !
They are: !
len()!lower()!upper()!
!
len()len() gets the length/ number of characters in a string.
Note that it is lowercase. !
Put the string you are trying to find the length of in the parenthesis.
The length is 35 if you were wondering.
lower()lower() converts a string to all lowercase.
Note that lower() is used by typing string.lower() This will not work with len()
upper()upper() converts a string to uppercase (surprise!)
upper() is also called using string.upper()
+ - / *• + is addition
• - is subtraction
• * is multiplication
• / is division
• These work the same way your calculator does -- no surprises here
** and %
• ** (exponent) raises one number to the power of another. For example, 2**3 would be 8 because 2*2*2=8
• % (modulus) returns the remainder of division. For example, 25%7 would be 4 because 25/7 is 3r4.
CommentsWhat if you write a really long program then don't look at it for a year? What if you're working on a project with other
people and you have to share your code? !
We need a way to explain what is going on in the programs we write.
!The solution? Comments.
How to make a commentComments are a tool we have to communicate what our
code is intended to do. !
To put a comment in your code all you have to do is type # your comment here!
!Anything that comes after '#' and is on the same line will be
ignored when the code is being run.
This is from a HW assignment of mine. The green lines are comments.
Use them! !
Note: this is not Python
What we covered• A little bit about Python
• How to use Koding.com
• Data types: numbers, string, and booleans
• Variables and naming conventions
• Printing
• String methods
• Math operators
• Comments
from random import choice !!!!!def find(lst, a): ! result = [] ! for i, x in enumerate(lst): ! if x == a: ! result.append(i) ! return result !!!!!words = ['rhythms', 'tree', 'constant', 'manager', 'training', 'hotel', 'destroy'] !word = choice(words) !count = 0 !chances = 4 !letterlist = list(word) !dashlist = [] !for l in letterlist: ! dashlist.append('-') !print 'Welcome to Hangman\n\nGuess the: ' + str(len(letterlist)) + ' letter word.\n' !print ' '.join(dashlist) + '\n\n' !while count <= chances: ! chancesleft = chances - count ! let = raw_input("Enter a letter: ") ! if let in letterlist: ! indexes = find(letterlist, let) ! for i in indexes: ! dashlist[i] = let ! print '\n' + ' '.join(dashlist) + '\n' + 'Good guess!\n' ! else: ! print 'Wrong! Try again!' ! count += 1 ! print 'Chances left: ' + str(chancesleft) + '\n' ! combined = ''.join(dashlist) ! if combined == word: ! print 'You win! The word was: ' + word ! break ! if count > chances: ! print 'Game over. You lose! The word was: ' + word ! break
Here is the code for hangman.py !
You don't need to understand this right now, but it's a cool example
of what you can do.