SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 
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
 
python classes in thane
python classes in thanepython classes in thane
python classes in thanefaizrashid1995
 

Semelhante a 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
 
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
 
python classes in thane
python classes in thanepython classes in thane
python classes in thane
 

Último

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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.