- Python 2
https://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/tree/master/
3 / 2 = 1
3 / 2.0 = 1.5
float(3) / 2 = 1.5
Another method is using
from __future__ import division
>>> 3/2
1.5
Python 3 doesn't need to use import
- Strings
Python 3
from __future__ import print_function
print('Hello World') vs Python 2 print 'Hello world'
len(' ') checks string length
s = 'Hello World'
# Grab everything past the first term all the way to the length of s which is len(s)
s[1:] => "ello World'
# Grab everything UP TO the 3rd index
s[:3]=>
'Hel'
#Everything
s[:] =>
'Hello World'
# Grab everything, but go in step sizes of 2
s[::2] =>
'HloWrd'
# We can use this to print a string backwards
s[::-1] => 'dlroW olleH'
- Python 2 More Strings
s = 'STRING'
1. print 'Place another string with a mod and s: %s' %(s)
Place another string with a mod and s: STRING
# %s is ruby equivalent #{}. but you need the $(name)
2. print 'Floating point numbers: %1.2f' %(13.144)
Floating point numbers: 13.14
# Floating point numbers use the format %n1.n2f where the n1 is the total minimum number of digits the string should contain. The n2 placeholder stands for how many numbers to show past the decimal point.
3. print 'Here is a number: %s. Here is a string: %s' %(123.1,'hi')
Here is a number: 123.1. Here is a string: hi
4. 'String here {var1} then also {var2}'.format(var1='something1',var2='something2')
- List
>>> l = [1,2,3]
>>> l.append('append me!')
>>> l
[1, 2, 3, 'append me!']
#.pop() is interesting because .pop in ruby always start from the back. In python I can pop it from the front with .pop(1)
>>> l.pop(0)
1
>>> l
[2, 3, 'append me!']
>>>
- Another noticeable difference of ruby and python is that changes are permanent in python. That makes you want to think twice before manipulating the data For eg. reverse
>>> l.reverse()
>>> l
['append me!', 3, 2]
- Looping in an array. The interesting is that you can define a function that is a loop. And when you call the function, it will loop it.
Here is an example:
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> first_col = [i[0] for i in matrix]
>>> first_col
[1, 4, 7]
- Hash
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123 OR my_dict['key1'] -= 123
my_dict['key1'] => 0
- Create key-values is same as ruby
# Create a new dictionary
d = {}
# Create a new key through assignment
d['animal'] = 'Dog'
# Can do this with any object
d['answer'] = 42
#Show
d
>>> {'animal': 'Dog', 'answer': 42}
- Getting the key and value is similar in ruby except python has .items()
# Create a typical dictionary
d = {'key1':1,'key2':2,'key3':3}
# Method to return a list of all keys
d.keys()
=>
['key3', 'key2', 'key1']
# Method to grab all values
d.values()
=>
[3, 2, 1]
# Method to return tuples of all items
d.items()
=>
[('key3', 3), ('key2', 2), ('key1', 1)]
In Python 3 this is in ascending order
>>> d.keys()
dict_keys(['key1', 'key2', 'key3'])
>>> d.values()
dict_values([1, 2, 3])
>>> d.items()
dict_items([('key1', 1), ('key2', 2), ('key3', 3)])
- Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar.
eg. t = (1,2,3)
https://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/tree/master/
3 / 2 = 1
3 / 2.0 = 1.5
float(3) / 2 = 1.5
Another method is using
from __future__ import division
>>> 3/2
1.5
Python 3 doesn't need to use import
- Strings
Python 3
from __future__ import print_function
print('Hello World') vs Python 2 print 'Hello world'
len(' ') checks string length
s = 'Hello World'
# Grab everything past the first term all the way to the length of s which is len(s)
s[1:] => "ello World'
# Grab everything UP TO the 3rd index
s[:3]=>
'Hel'
#Everything
s[:] =>
'Hello World'
# Grab everything, but go in step sizes of 2
s[::2] =>
'HloWrd'
# We can use this to print a string backwards
s[::-1] => 'dlroW olleH'
- Python 2 More Strings
s = 'STRING'
1. print 'Place another string with a mod and s: %s' %(s)
Place another string with a mod and s: STRING
# %s is ruby equivalent #{}. but you need the $(name)
2. print 'Floating point numbers: %1.2f' %(13.144)
Floating point numbers: 13.14
# Floating point numbers use the format %n1.n2f where the n1 is the total minimum number of digits the string should contain. The n2 placeholder stands for how many numbers to show past the decimal point.
3. print 'Here is a number: %s. Here is a string: %s' %(123.1,'hi')
Here is a number: 123.1. Here is a string: hi
4. 'String here {var1} then also {var2}'.format(var1='something1',var2='something2')
- List
>>> l = [1,2,3]
>>> l.append('append me!')
>>> l
[1, 2, 3, 'append me!']
#.pop() is interesting because .pop in ruby always start from the back. In python I can pop it from the front with .pop(1)
>>> l.pop(0)
1
>>> l
[2, 3, 'append me!']
>>>
- Another noticeable difference of ruby and python is that changes are permanent in python. That makes you want to think twice before manipulating the data For eg. reverse
>>> l.reverse()
>>> l
['append me!', 3, 2]
- Looping in an array. The interesting is that you can define a function that is a loop. And when you call the function, it will loop it.
Here is an example:
>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> first_col = [i[0] for i in matrix]
>>> first_col
[1, 4, 7]
- Hash
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123 OR my_dict['key1'] -= 123
my_dict['key1'] => 0
- Create key-values is same as ruby
# Create a new dictionary
d = {}
# Create a new key through assignment
d['animal'] = 'Dog'
# Can do this with any object
d['answer'] = 42
#Show
d
>>> {'animal': 'Dog', 'answer': 42}
- Getting the key and value is similar in ruby except python has .items()
# Create a typical dictionary
d = {'key1':1,'key2':2,'key3':3}
# Method to return a list of all keys
d.keys()
=>
['key3', 'key2', 'key1']
# Method to grab all values
d.values()
=>
[3, 2, 1]
# Method to return tuples of all items
d.items()
=>
[('key3', 3), ('key2', 2), ('key1', 1)]
In Python 3 this is in ascending order
>>> d.keys()
dict_keys(['key1', 'key2', 'key3'])
>>> d.values()
dict_values([1, 2, 3])
>>> d.items()
dict_items([('key1', 1), ('key2', 2), ('key3', 3)])
- Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar.
eg. t = (1,2,3)
Comments
Post a Comment