SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
ACM init()
Day 1: April 15, 2015
1
Monty
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: omalleyk@ucla.edu
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/
The numbers speak for
themselves...
Source: http://code.org/promote
TIOBE Index
TIOBE Index
The 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.
Factorial
A bit about computers
Computers 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.
koding.com
Enter your email and choose a username
This is what you should see once you log in
Running your first Python program: type print "Hello world!"
into the top box
Now, save the file. Select the option Save As.
Name the file 'hello.py,' then click Save! Always use '.py' for
Python files.
Now we'll run it! Type python hello.py in the bottom window.
Press enter, and the program will run!
That's all it takes! All Python programs will be run using
python filename.py
Now you know how to run
Python programs. Let's
learn how to write them!
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 types
It's important to know the difference between data types
because they are written and used differently.
5 = number
"5" = string
!
True = boolean
"True" = string
Variables
We 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!
Variables
Some 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: printing
We'll often want to print to the computer screen. The command
for this is print
More about printing
What 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.
Strings are special
Remember, a string is a word or phrase.
It's always written in " "
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 methods
Python 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()
Math
There are six main math operations we are going to go over
today:
+ - / *
• + 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.
Comments
What 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 comment
Comments 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.
Comment example
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
A cool example: hangman.py
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 HangmannnGuess the: ' + str(len(letterlist)) + ' letter word.n'
!print ' '.join(dashlist) + 'nn'
!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.

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Part 2 Python
Part 2 PythonPart 2 Python
Part 2 Python
 
Anti-Patterns
Anti-PatternsAnti-Patterns
Anti-Patterns
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Basic syntax supported by python
Basic syntax supported by pythonBasic syntax supported by python
Basic syntax supported by python
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Python introduction
Python introductionPython introduction
Python introduction
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introduction
 
Python Basics
Python BasicsPython Basics
Python Basics
 
An Introduction To Python - FOR Loop
An Introduction To Python - FOR LoopAn Introduction To Python - FOR Loop
An Introduction To Python - FOR Loop
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityPython-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutability
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 
Variables and Expressions
Variables and ExpressionsVariables and Expressions
Variables and Expressions
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
pyton Notes1
pyton Notes1pyton Notes1
pyton Notes1
 

Andere mochten auch

Angora Gardens Classic Car Show
Angora Gardens Classic Car ShowAngora Gardens Classic Car Show
Angora Gardens Classic Car ShowDarlene Williams
 
P1 induction comely park
P1 induction comely parkP1 induction comely park
P1 induction comely parksusanmcintosh5
 
Batidos saludables (1)
Batidos saludables (1)Batidos saludables (1)
Batidos saludables (1)sala9170
 
Aif l and raising attainment
Aif l and raising attainmentAif l and raising attainment
Aif l and raising attainmentcurriechs
 
Digital citizenship number 6
Digital citizenship number 6Digital citizenship number 6
Digital citizenship number 6Siegmeyer
 

Andere mochten auch (20)

ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
ACM init() Day 3
ACM init() Day 3ACM init() Day 3
ACM init() Day 3
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 
ACM init() Day 5
ACM init() Day 5ACM init() Day 5
ACM init() Day 5
 
ACM Init() lesson 1
ACM Init() lesson 1ACM Init() lesson 1
ACM Init() lesson 1
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 
Angora Gardens Car Show
Angora Gardens Car Show Angora Gardens Car Show
Angora Gardens Car Show
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
Angora Gardens Classic Car Show
Angora Gardens Classic Car ShowAngora Gardens Classic Car Show
Angora Gardens Classic Car Show
 
UCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meetingUCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meeting
 
ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
UCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your DomainUCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your Domain
 
P1 induction comely park
P1 induction comely parkP1 induction comely park
P1 induction comely park
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
Batidos saludables (1)
Batidos saludables (1)Batidos saludables (1)
Batidos saludables (1)
 
Seminario 5
Seminario 5Seminario 5
Seminario 5
 
Aif l and raising attainment
Aif l and raising attainmentAif l and raising attainment
Aif l and raising attainment
 
Digital citizenship number 6
Digital citizenship number 6Digital citizenship number 6
Digital citizenship number 6
 

Ähnlich wie ACM init() Spring 2015 Day 1

Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptxHaythamBarakeh1
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsRuth Marvin
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style GuidesMosky Liu
 
Introduction to Python programming 1.pptx
Introduction to Python programming 1.pptxIntroduction to Python programming 1.pptx
Introduction to Python programming 1.pptxJoshuaAnnan5
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Lucky Gods
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computingGo Asgard
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfMichpice
 
python introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxpython introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxChandraPrakash715640
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Amr Alaa El Deen
 

Ähnlich wie ACM init() Spring 2015 Day 1 (20)

python.pdf
python.pdfpython.pdf
python.pdf
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style Guides
 
Introduction to Python programming 1.pptx
Introduction to Python programming 1.pptxIntroduction to Python programming 1.pptx
Introduction to Python programming 1.pptx
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
python_class.pptx
python_class.pptxpython_class.pptx
python_class.pptx
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
How To Tame Python
How To Tame PythonHow To Tame Python
How To Tame Python
 
#Code2Create: Python Basics
#Code2Create: Python Basics#Code2Create: Python Basics
#Code2Create: Python Basics
 
Python PPT.pptx
Python PPT.pptxPython PPT.pptx
Python PPT.pptx
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
 
python introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxpython introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptx
 
First session
First sessionFirst session
First session
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 

Kürzlich hochgeladen

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 

Kürzlich hochgeladen (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

ACM init() Spring 2015 Day 1

  • 1. ACM init() Day 1: April 15, 2015 1 Monty
  • 2. 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
  • 3. 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
  • 4. 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: omalleyk@ucla.edu
  • 5. 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/
  • 6. The numbers speak for themselves... Source: http://code.org/promote
  • 8. TIOBE Index The 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.
  • 9. 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!
  • 10. 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.
  • 12. A bit about computers Computers 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.
  • 13. 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.
  • 15. Enter your email and choose a username
  • 16. This is what you should see once you log in
  • 17. Running your first Python program: type print "Hello world!" into the top box
  • 18. Now, save the file. Select the option Save As.
  • 19. Name the file 'hello.py,' then click Save! Always use '.py' for Python files.
  • 20. Now we'll run it! Type python hello.py in the bottom window.
  • 21. Press enter, and the program will run! That's all it takes! All Python programs will be run using python filename.py
  • 22. Now you know how to run Python programs. Let's learn how to write them!
  • 23. 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!")
  • 24. Data types It's important to know the difference between data types because they are written and used differently. 5 = number "5" = string ! True = boolean "True" = string
  • 25. Variables We 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!
  • 26. Variables Some 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."
  • 27. 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
  • 28. Quick topic: printing We'll often want to print to the computer screen. The command for this is print
  • 29. More about printing What 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.
  • 30. Strings are special Remember, a string is a word or phrase. It's always written in " "
  • 31. 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!
  • 32. String methods Python has some special built-in functions that we can use for strings. ! They are: ! len()! lower()! upper()! !
  • 33. 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.
  • 34. lower() lower() converts a string to all lowercase. Note that lower() is used by typing string.lower() This will not work with len()
  • 35. upper() upper() converts a string to uppercase (surprise!) upper() is also called using string.upper()
  • 36. Math There are six main math operations we are going to go over today:
  • 37. + - / * • + is addition • - is subtraction • * is multiplication • / is division • These work the same way your calculator does -- no surprises here
  • 38. ** 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.
  • 39. Comments What 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.
  • 40. How to make a comment Comments 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.
  • 42. This is from a HW assignment of mine. The green lines are comments. Use them! ! Note: this is not Python
  • 43. 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
  • 44. A cool example: hangman.py
  • 45. 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 HangmannnGuess the: ' + str(len(letterlist)) + ' letter word.n' !print ' '.join(dashlist) + 'nn' !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.