SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Python Tidbits
Mitchell Vitez
● Learn new constructs
● Play with them in the interpreter
● Learn how to apply them to your own code
● Become a better Python programmer
Goals
import this
import antigravity
from __future__ import braces
if condition:
pass
#{
#}
# -*- coding: rot13 -*-
cevag 'Clguba vf sha!'
Chained comparisons
if 12 < x < 144:
print x
3 != x < y == 24 < z < 1000 > pi
Swapping
Temporary Variable
a, b = 1, 2
temp = a
a = b
b = temp
XOR
a, b = 1, 2
a ^= b
b ^= a
a ^= b
Problem
a, b = 'Python', 'Tidbits'
Solution
a, b = b, a
Tuple packing
a, b = 'it will', 'consume you'
a, b = b, a
(a, b) = (b, a)
(a, b) = ('consume you', 'it will')
temp = a
a = b
b = temp
a, b = b, a
point = (1, 2, 3)
x, y, z = point
nums = 1, 2, 3, 4, 5
sum(nums)
Slices
[start:end:step]
'Michigan Hackers'[6:13]
'Michigan Hackers'[6:13]
0123456789abcdef
'Michigan Hackers'[:-5]
0123456789abcdef
'Michigan Hackers'[-5:]
0123456789abcdef
'Michigan Hackers'[::2]
0123456789abcdef
[::-1]
reversed()
List comprehensions
Make a list of cubes
of the first 100
numbers
Imperative
cubes = []
for x in range(100):
cubes.append(x ** 3)
Functional
cubes = map(lambda x: x ** 3, range(100))
List comprehension
cubes = [x ** 3 for x in range(100)]
With a predicate
cubes = []
for x in range(100):
if x % 2:
cubes.append(x ** 3)
cubes = map(lambda x: x ** 3,
filter(lambda x: x % 2, range(100)))
cubes = [x ** 3 for x in range(100) if x % 2]
cubes = [x ** 3 for x in range(100)][1::2]
Sum of two dice
table = []
for i in range(1, 7):
row = []
for j in range(1, 7):
row.append(i + j)
table.append(row)
[[i + j for i in range(1, 7)]
for j in range(1, 7)]
word = 'abracadabra'
[word[:i] for i in range(len(word), 0, -1)]
Splat operator
Named arguments
def increase(num, how_much=1, multiply=False):
if multiply:
return num * how_much
return num + how_much
increase(1)
increase(10, 10, False)
increase(25, multiply=True, how_much=2)
Unpack dictionaries to arguments
options = {
'is_the_splab_door_open': True,
'is_it_hack_night_yet': False,
'students_enrolled_at_umich': 43651,
'fishythefish_status': 'savage'
}
func(**options)
Unpack argument lists
def add(a, b):
return a + b
nums = [1, 2]
add(*nums)
Variadic functions
def sum(*nums):
result = 0
for num in nums:
result += num
return result
sum(1, 2, 3, 4)
Args and kwargs
def function(arg1, arg2, *args, **kwargs):
pass
Decomposition
x, xs = things[0], things[1:]
x, *xs = things
Iterators
for _ in _
for item in list
for character in string
for key in dictionary
for line in file
__iter__
nums = [1, 2, 3, 4, 5]
iterator = iter(nums)
iterator.next()
Implementing range
class simple_range:
def __init__(self, n):
self.n = n
self.data = [i for i in range(n)]
def __iter__(self):
return iter(self.data)
Performance
import time
class simple_range:
def __init__(self, n):
t0 = time.time()
self.n = n
self.data = [i for i in range(n)]
print 'Time taken:', time.time() - t0
def __iter__(self):
return iter(self.data)
simple_range(100000000)
# Time taken: 7.59687685966
Improved Performance
class simple_range:
def __init__(self, n):
self.n = n
self.i = 0
def __iter__(self):
return self
def next(self):
if self.i < self.n:
result = self.i
self.i += 1
return result
else:
raise StopIteration()
range, xrange, Python 3 range
simple_range(100000000)
# Time taken: 7.59687685966
# Time taken: 0.00002408027
Generators
Functions returning sequences
def simple_range(n):
i = 0
while i < n:
yield i
i += 1
yielding
Generator expressions
cubes = (x ** 3 for x in range(100))
How many 3-letter strings are
alphabetically between 'foo' and 'bar'?
A more complicated generator
def alphagen(start, end=None):
s = list(reversed(start))
while end is None or s != list(reversed(end)):
yield ''.join(reversed(s))
if s == list('z' * len(s)):
s = list('a' * (len(s) + 1))
else:
for i, ch in enumerate(s):
if ch is 'z':
s[i] = 'a'
else:
s[i] = chr(ord(ch) + 1)
break
Solution
i = 0
for a in alphagen('bar', 'foo'):
i += 1
print i - 1
# Time taken to create generator: .00000596046
Decorators
Tracing
def fibonacci(n):
t0 = time.time()
print 'fib started'
if n is 0 or n is 1:
print 'fib finished in', time.time() - t0,
'seconds returning', 1
return 1
else:
x = fibonacci(n - 1) + fibonacci(n - 2)
print 'fib finished in', time.time() - t0,
'seconds returning', x
return x
Tracing
def trace(f):
def f_prime(n):
t0 = time.time()
print f.__name__, 'started'
value = f(n)
print f.__name__, 'finished in', time.time() -
t0, 'seconds returning', value
return value
return f_prime
def fibonacci(n):
if n is 0 or n is 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
fibonacci = trace(fibonacci)
Syntactic sugar
@trace
def fibonacci(n):
if n is 0 or n is 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
Tidbits
s = u'Hallelujah Bovay Alabama Δ'
words = s.split()
s = ', '.join(words)
print s
' '.join([''.join(reversed(word)) for word in s.split()])
' '.join(reversed(s.split()))
reversed(s)
import random
random.choice(['Ankit', 'Abby', 'Edward',
'Andrew', 'Omkar', 'Jason'])
Interested in more? Check out these
dis
pdb
flask
pydoc
pygame
itertools
functools
exec, eval
beautiful soup
dict comprehensions
Python Tidbits

Weitere ähnliche Inhalte

Was ist angesagt?

2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
Taketo Sano
 
Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better Code
Stronnics
 
Matematika kelompok
Matematika kelompokMatematika kelompok
Matematika kelompok
vanniaamelda
 

Was ist angesagt? (20)

2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
Pdfcode
PdfcodePdfcode
Pdfcode
 
Share test
Share testShare test
Share test
 
Asynchronous programming with java script and node.js
Asynchronous programming with java script and node.jsAsynchronous programming with java script and node.js
Asynchronous programming with java script and node.js
 
Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''
 
Basic Calculus in R.
Basic Calculus in R. Basic Calculus in R.
Basic Calculus in R.
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
Codecomparaison
CodecomparaisonCodecomparaison
Codecomparaison
 
Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better Code
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
Frsa
FrsaFrsa
Frsa
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
Matematika kelompok
Matematika kelompokMatematika kelompok
Matematika kelompok
 
DeepLearning ハンズオン資料 20161220
DeepLearning ハンズオン資料 20161220DeepLearning ハンズオン資料 20161220
DeepLearning ハンズオン資料 20161220
 
Asynchronous programming and mutlithreading
Asynchronous programming and mutlithreadingAsynchronous programming and mutlithreading
Asynchronous programming and mutlithreading
 
Let's golang
Let's golangLet's golang
Let's golang
 

Ähnlich wie Python Tidbits

Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
kesav24
 
Python-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdfPython-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdf
letsdism
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 

Ähnlich wie Python Tidbits (20)

Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Python-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdfPython-3-compiled-Cheat-Sheet-version-3.pdf
Python-3-compiled-Cheat-Sheet-version-3.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python in 90mins
Python in 90minsPython in 90mins
Python in 90mins
 
A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
Teeing Up Python - Code Golf
Teeing Up Python - Code GolfTeeing Up Python - Code Golf
Teeing Up Python - Code Golf
 
bobok
bobokbobok
bobok
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Python Tidbits

Hinweis der Redaktion

  1. View the code at https://github.com/mitchellvitez/Python-Tidbits
  2. TypeError: unsupported operand type(s) for ^=: 'str' and 'str'