SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
 Write following python script with your text
editor.
 Then, run ‘python your_script.py’
 If you got ‘Hello World’, go on to the next
slide.
def main():
print ‘Hello World’
if __name__ == ‘__main__’:
main()
 Lists – compound data type
l = [1,2,3,5] # defines a list contains 1, 2, 3, 5
print l[0] # getting a value
l[0] = 6 # setting a value
l.append(7) # adding to list
print l # will print ‘[6, 2, 3, 5, 7]’
l.extend([8, 9, 10]) # connecting another list
print l # will print ‘[6, 2, 3, 5, 7, 8, 9, 10]’
print range(3) # stops at 2
print range(1, 3) # starts at 1 and stops at 2
print range(0, 5, 2) # start: 1 stop: 4 and steps by 2
 Create following lists with range function.
1. [0, 1, 2, 3, 4]
2. [3, 4, 5, 6, 7]
3. [3, 6, 9, 12, 15, 18]
4. [2, 4, 6, 8, 12, 16, 20] hint: use + operator
 Answer:
1. range(5)
2. range(3, 8)
3. range(3, 21, 3)
4. range(2, 10, 2) + range(12, 24, 4)
 Dictionaries – compound data type
 Found in other languages as “map”,
“associative memories”, or “associative arrays”
 Lists vs Dictionaries
› You can use only integer number as index on lists
Like a[0], a[-1]
› You can use integer numbers and strings as key on
dictionaries(if its value exists)
Like d[0], d[‘foo’], d[‘bar’]
 Creating a dictionary
tel = {‘John’:8000, ‘Jane’:8001, ‘Joe’:8002}
 Adding key and value
tel[‘Joan’] = 8003
 Getting value from key
tel[‘Jane’]
 Setting value from key
tel[‘Joe’] = 0004
 Removing value from key
del tel[‘John’]
 Getting key list of a dictionary
tel.keys()
 Crate a dictionary from two lists
names = [‘John’, ‘Jane’, ‘Joe’]
tel = [8000, 8001, 8002]
tel = dict(zip(names, tel))
 Create a dictionary that
› Keys are [0, 1, …, 10]
› Values are (key) * 2
› i.e. d[0]=0, d[1]=2, d[2]=4, …, d[10]=20
 Answer:
k = range(11)
v = range(0, 11*2, 2)
d = dict(zip(k, v))
 Logical Operator
 if statements
 for statements
 Compares two values
print 1 < 2
print 1 > 2
print 1 == 2
print 1 <= 2
print 1 >= 2
 And, Or, and Not
print 1 < 2 and 1 == 2
print 1 < 2 or 1 > 2
print not 1 == 2
x = int(raw_input(‘value of x: ’))
if x < 0:
print(‘x is negative’) # must indent
elif x > 0:
print(‘x is positive’)
else:
print(‘x is not positive neither negative’)
print(‘i.e. x is zero’)
For integer variable named ‘x’,
› Print ‘fizz’ if x can be divided by 3
› Print ‘buzz’ if x can be divided by 5
(e.g. if x is 10, print ‘buzz’, x is 15, print ‘fizz buzz’)
(hint: use % operator(e.g. 10 % 3 = 1, 14 % 5 = 4))
x = int(raw_input('x value:'))
if x % 3 == 0:
print 'fizz'
if x % 5 == 0:
print 'buzz'
n = int(raw_input(‘list length: ’))
l = []
for i in range(n): # for each number in (0…n-1)
l.append(raw_input(‘%dth word: ’ % i))
for w in l: # for each word in your list
print w
Create a list only contains
› Multiple of 3 or 5 (3, 5, 6, 9…)
› Between 1 and 20
i.e. [3, 5, 6, 9, 10, 12, 15, 18, 20]
l = []
for i in range(1, 21):
if i % 3 == 0 or i % 5 == 0:
l.append(i)
print l
 Defining a function
def add(a, b):
return a + b
 Calling the function
print add(10, 2)
 Recursive call
def factorial(n):
if n <= 0:
return 1
else:
return n * factorial(n-1)
 Find the 10-th Fibonacci sequence
› Fibonacci sequence is
 0, 1, 1, 2, 3, 5, 8, …
 The next number is found by adding up the two
numbers before it(e.g. 5th number 3 = 2 + 1).
def fib(n):
if n <= 1:
return 0
elif n == 2:
return 1
else:
return fib(n-1) + fib(n-2)
print fib(10)

Mais conteúdo relacionado

Mais procurados

令和から本気出す
令和から本気出す令和から本気出す
令和から本気出すTakashi Kitano
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsDavid Golden
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitTobias Pfeiffer
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitTobias Pfeiffer
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析Takashi Kitano
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackVic Metcalfe
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On ListsIHTMINSTITUTE
 
Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発TipsTakashi Kitano
 
Favouring Composition - The Groovy Way
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy WayNaresha K
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPandrewnacin
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)Takashi Kitano
 
Database performance 101
Database performance 101Database performance 101
Database performance 101Leon Fayer
 
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Codemotion
 

Mais procurados (20)

令和から本気出す
令和から本気出す令和から本気出す
令和から本気出す
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit Testing
 
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
{tidytext}と{RMeCab}によるモダンな日本語テキスト分析
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 
Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"
 
Python idioms
Python idiomsPython idioms
Python idioms
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips{shiny}と{leaflet}による地図アプリ開発Tips
{shiny}と{leaflet}による地図アプリ開発Tips
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
Favouring Composition - The Groovy Way
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy Way
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver){tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
{tidygraph}と{ggraph}による モダンなネットワーク分析(未公開ver)
 
Database performance 101
Database performance 101Database performance 101
Database performance 101
 
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
 

Destaque

How Docker didn't invent containers (Docker Meetup Brno #1)
How Docker didn't invent containers (Docker Meetup Brno #1)How Docker didn't invent containers (Docker Meetup Brno #1)
How Docker didn't invent containers (Docker Meetup Brno #1)Pavel Snajdr
 
KEPERCAYAAN GURU
KEPERCAYAAN GURUKEPERCAYAAN GURU
KEPERCAYAAN GURUNurAlias91
 
công ty thiết kế phim quảng cáo nhanh nhất
công ty thiết kế phim quảng cáo nhanh nhấtcông ty thiết kế phim quảng cáo nhanh nhất
công ty thiết kế phim quảng cáo nhanh nhấtevette568
 
Top 8 internet marketing specialist resume samples
Top 8 internet marketing specialist resume samplesTop 8 internet marketing specialist resume samples
Top 8 internet marketing specialist resume sampleshallerharry710
 
Terril Application Developer
Terril Application DeveloperTerril Application Developer
Terril Application DeveloperTerril Thomas
 
when you forget your password
when you forget your passwordwhen you forget your password
when you forget your passwordCorneliu Dascălu
 
Advertising agencies
Advertising agenciesAdvertising agencies
Advertising agenciesShrey Oberoi
 

Destaque (14)

Oratoria
OratoriaOratoria
Oratoria
 
Pelajaran5
Pelajaran5Pelajaran5
Pelajaran5
 
Pelajaran4
Pelajaran4Pelajaran4
Pelajaran4
 
How Docker didn't invent containers (Docker Meetup Brno #1)
How Docker didn't invent containers (Docker Meetup Brno #1)How Docker didn't invent containers (Docker Meetup Brno #1)
How Docker didn't invent containers (Docker Meetup Brno #1)
 
KEPERCAYAAN GURU
KEPERCAYAAN GURUKEPERCAYAAN GURU
KEPERCAYAAN GURU
 
công ty thiết kế phim quảng cáo nhanh nhất
công ty thiết kế phim quảng cáo nhanh nhấtcông ty thiết kế phim quảng cáo nhanh nhất
công ty thiết kế phim quảng cáo nhanh nhất
 
Top 8 internet marketing specialist resume samples
Top 8 internet marketing specialist resume samplesTop 8 internet marketing specialist resume samples
Top 8 internet marketing specialist resume samples
 
Galaxy royale
Galaxy royaleGalaxy royale
Galaxy royale
 
Ace city
 Ace city  Ace city
Ace city
 
La Representacion Grafica
La Representacion GraficaLa Representacion Grafica
La Representacion Grafica
 
Carlito N. Siervo Jr
Carlito N. Siervo JrCarlito N. Siervo Jr
Carlito N. Siervo Jr
 
Terril Application Developer
Terril Application DeveloperTerril Application Developer
Terril Application Developer
 
when you forget your password
when you forget your passwordwhen you forget your password
when you forget your password
 
Advertising agencies
Advertising agenciesAdvertising agencies
Advertising agencies
 

Semelhante a PyLecture4 -Python Basics2-

第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎juzihua1102
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎anzhong70
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Python basic
Python basic Python basic
Python basic sewoo lee
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILEAnushka Rai
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxHongAnhNguyn285885
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfomprakashmeena48
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningTURAGAVIJAYAAKASH
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat SheetVerxus
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfElNew2
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School ProgrammersSiva Arunachalam
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptxChandanVatsa2
 

Semelhante a PyLecture4 -Python Basics2- (20)

第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Python basic
Python basic Python basic
Python basic
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
Python slide
Python slidePython slide
Python slide
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdf
 
Practical File Grade 12.pdf
Practical File Grade 12.pdfPractical File Grade 12.pdf
Practical File Grade 12.pdf
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
Python : Dictionaries
Python : DictionariesPython : Dictionaries
Python : Dictionaries
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
Python
PythonPython
Python
 

Mais de Yoshiki Satotani

Mais de Yoshiki Satotani (7)

Basic practice of R
Basic practice of RBasic practice of R
Basic practice of R
 
Basic use of Python (Practice)
Basic use of Python (Practice)Basic use of Python (Practice)
Basic use of Python (Practice)
 
Basic use of Python
Basic use of PythonBasic use of Python
Basic use of Python
 
Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
PyLecture2 -NetworkX-
PyLecture2 -NetworkX-PyLecture2 -NetworkX-
PyLecture2 -NetworkX-
 
PyLecture1 -Python Basics-
PyLecture1 -Python Basics-PyLecture1 -Python Basics-
PyLecture1 -Python Basics-
 
PyLecture3 -json-
PyLecture3 -json-PyLecture3 -json-
PyLecture3 -json-
 

Último

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Último (20)

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

PyLecture4 -Python Basics2-

  • 1.
  • 2.  Write following python script with your text editor.  Then, run ‘python your_script.py’  If you got ‘Hello World’, go on to the next slide. def main(): print ‘Hello World’ if __name__ == ‘__main__’: main()
  • 3.  Lists – compound data type l = [1,2,3,5] # defines a list contains 1, 2, 3, 5 print l[0] # getting a value l[0] = 6 # setting a value l.append(7) # adding to list print l # will print ‘[6, 2, 3, 5, 7]’ l.extend([8, 9, 10]) # connecting another list print l # will print ‘[6, 2, 3, 5, 7, 8, 9, 10]’
  • 4. print range(3) # stops at 2 print range(1, 3) # starts at 1 and stops at 2 print range(0, 5, 2) # start: 1 stop: 4 and steps by 2
  • 5.  Create following lists with range function. 1. [0, 1, 2, 3, 4] 2. [3, 4, 5, 6, 7] 3. [3, 6, 9, 12, 15, 18] 4. [2, 4, 6, 8, 12, 16, 20] hint: use + operator
  • 6.  Answer: 1. range(5) 2. range(3, 8) 3. range(3, 21, 3) 4. range(2, 10, 2) + range(12, 24, 4)
  • 7.  Dictionaries – compound data type  Found in other languages as “map”, “associative memories”, or “associative arrays”  Lists vs Dictionaries › You can use only integer number as index on lists Like a[0], a[-1] › You can use integer numbers and strings as key on dictionaries(if its value exists) Like d[0], d[‘foo’], d[‘bar’]
  • 8.  Creating a dictionary tel = {‘John’:8000, ‘Jane’:8001, ‘Joe’:8002}  Adding key and value tel[‘Joan’] = 8003  Getting value from key tel[‘Jane’]  Setting value from key tel[‘Joe’] = 0004  Removing value from key del tel[‘John’]  Getting key list of a dictionary tel.keys()
  • 9.  Crate a dictionary from two lists names = [‘John’, ‘Jane’, ‘Joe’] tel = [8000, 8001, 8002] tel = dict(zip(names, tel))
  • 10.  Create a dictionary that › Keys are [0, 1, …, 10] › Values are (key) * 2 › i.e. d[0]=0, d[1]=2, d[2]=4, …, d[10]=20
  • 11.  Answer: k = range(11) v = range(0, 11*2, 2) d = dict(zip(k, v))
  • 12.  Logical Operator  if statements  for statements
  • 13.  Compares two values print 1 < 2 print 1 > 2 print 1 == 2 print 1 <= 2 print 1 >= 2  And, Or, and Not print 1 < 2 and 1 == 2 print 1 < 2 or 1 > 2 print not 1 == 2
  • 14. x = int(raw_input(‘value of x: ’)) if x < 0: print(‘x is negative’) # must indent elif x > 0: print(‘x is positive’) else: print(‘x is not positive neither negative’) print(‘i.e. x is zero’)
  • 15. For integer variable named ‘x’, › Print ‘fizz’ if x can be divided by 3 › Print ‘buzz’ if x can be divided by 5 (e.g. if x is 10, print ‘buzz’, x is 15, print ‘fizz buzz’) (hint: use % operator(e.g. 10 % 3 = 1, 14 % 5 = 4))
  • 16. x = int(raw_input('x value:')) if x % 3 == 0: print 'fizz' if x % 5 == 0: print 'buzz'
  • 17. n = int(raw_input(‘list length: ’)) l = [] for i in range(n): # for each number in (0…n-1) l.append(raw_input(‘%dth word: ’ % i)) for w in l: # for each word in your list print w
  • 18. Create a list only contains › Multiple of 3 or 5 (3, 5, 6, 9…) › Between 1 and 20 i.e. [3, 5, 6, 9, 10, 12, 15, 18, 20]
  • 19. l = [] for i in range(1, 21): if i % 3 == 0 or i % 5 == 0: l.append(i) print l
  • 20.  Defining a function def add(a, b): return a + b  Calling the function print add(10, 2)
  • 21.  Recursive call def factorial(n): if n <= 0: return 1 else: return n * factorial(n-1)
  • 22.  Find the 10-th Fibonacci sequence › Fibonacci sequence is  0, 1, 1, 2, 3, 5, 8, …  The next number is found by adding up the two numbers before it(e.g. 5th number 3 = 2 + 1).
  • 23. def fib(n): if n <= 1: return 0 elif n == 2: return 1 else: return fib(n-1) + fib(n-2) print fib(10)