Monty Python's Flying Circus
The best way (in my opinion) to install Python is through Anaconda
bash /path/to/file/Anaconda3-5.2.0-Linux-x86_64.sh
Once you've installed Python correctly, you shold be able to
ipython
victor2@Victors-MBP-3:~ ipython
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
For more information on how to install Python
see:
python
or ipython
on the command line:
victor2@Victors-MBP-3:~ ipython
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
Ctrl-d, exit()
or quit()
victor2@Victors-MBP-3:~ ipython
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: exit
victor2@Victors-MBP-3:~
<var> = <expression>
victor2@Victors-MBP-3:~ ipython
nuPython 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: number = 5
In [2]: number
Out[2]: 5
x
and y
In [1]: x = 5
In [2]: x
Out[2]: 5
In [3]: y = 12
In [4]: y
Out[4]: 12
In [5]: x = y
In [6]: y = x
In [7]: x, y
Out[7]: (12, 12)
x
and y
In [1]: x = 5
In [2]: x
Out[2]: 5
In [3]: y = 12
In [4]: y
Out[4]: 12
In [5]: temp = x
In [6]: x = y
In [7]: y = temp
In [8]: x, y
Out[7]: (12, 5)
<var1>, <var2>, ... = <expr1>, <expr2>, ...
In [1]: x, y = 5, 12
In [2]: x
Out[2]: 5
In [3]: y
Out[3]: 12
x
and y
In [1]: x, y = 5, 12
In [2]: x, y = y, x
In [3]: x,y
Out[3]: (12, 5)
input
:
In [1]: name = input("What is your name?")
What is your name?"Victor"
In [2]: print("Hello", name)
Hello "Victor"
name.
+ | Additon |
- | Subtraction |
* | Multiplicaiton |
/ | Divison |
** | Exponents |
% | Remainder |
abs() | Absolute value |
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
≥ | Greather than or equal to |
≤ | Less than or equal to |
and | Logical 'and' |
or | Logical 'or' |
Examples
In [1]: 2 + 2 * 3
Out[1]: 8
In [2]: (2 + 2) * 3
Out[2]: 12
In [3]: 3.1415 * 2
Out[3]: 6.283
In [4]: 5/2
Out[4]: 2.5
In [5]: 2**100
Out[5]: 1267650600228229401496703205376
Spam
, eggs
, and spam and eggs
In [1]: S = "Spam"
len(string_name)
In [1]: S = "Spam"
In [2]: len(S)
Out[2]: 4
string_name[index]
In [1]: S = "Spam"
In [2]: len(S)
Out[2]: 4
In [3]: S[0]
Out[3]: 'S'
In [4]: S[2]
Out[4]: 'a'
In [5]: S[-1]
Out[5]: 'm'
string_name[begin:end]
Try: S[1:3]
In [1]: S = "Spam"
In [2]: len(S)
Out[2]: 4
In [3]: S[1:3]
Out[3]: 'pa'
+
"*
"Try: S[1:3]
In [1]: S = "Spam"
In [2]: len(S)
Out[2]: 4
In [3]: S + 'Eggs'
Out[3]: 'SpamEggs'
In [4]: S * 4
Out[4]: 'SpamSpamSpamSpam'
L[1]
and L[0:-1]
In [1]: L = [1, 'spam', 4.0, 'U']
In [2]: L
Out[2]: [1, 'spam', 4.0, 'U']
In [3]: L[1]
Out[3]: 'spam'
In [4]: L[0:-1]
Out[4]: [1, 'spam', 4.0]
L.append('eggs')
and L[3]
In [1]: L = [1, 'spam', 4.0, 'U']
In [2]: L
Out[2]: [1, 'spam', 4.0, 'U']
In [5]: L.append('eggs')
In [6]: L
Out[6]: [1, 'spam', 4.0, 'U', 'eggs']
In [7]: del L[3]
In [8]: L
Out[8]: [1, 'spam', 4.0, 'eggs']
In [1]: M = []
[1, 2, 3]
In [1]: M = []
In [2]: M.append([1, 2, 3])
In [3]: M
Out[3]: [[1, 2, 3]]
[4, 5, 6]
In [1]: M = []
In [2]: M.append([1,2,3])
In [3]: M
Out[3]: [[1, 2, 3]]
In [4]: M.append([4, 5, 6])
In [5]: M
Out[5]: [[1, 2, 3], [4, 5, 6]]
In [1]: M = []
In [2]: M.append([1,2,3])
In [3]: M
Out[3]: [[1, 2, 3]]
In [4]: M.append([4, 5, 6])
In [5]: M
Out[5]: [[1, 2, 3], [4, 5, 6]]
In [6]: M[0][2]
Out[6]: 3
In [1]: A = []
In [2]: A.append([1, 2, 3])
In [3]: A
Out[3]: [[1, 2, 3]]
In [4]: A.append(['spam', 'eggs'])
In [5]: A
Out[5]: [[1, 2, 3], ['spam', 'eggs']]
In [6]: A.append([4.0, 'spam', [1, 2, 'string'], 5])
In [7]: A
Out[7]: [[1, 2, 3], ['spam', 'eggs'], [4.0, 'spam', [1, 2, 'string'], 5]]
(1, 'spam', 4.0, 'U')
In [1]: T = (1, 'spam', 4.0, 'U')
len(T); T + (5,6); T[1]
In [1]: T = (1, 'spam', 4.0, 'U')
In [2]: len(T)
Out[2]: 4
In [3]: T + (5,6)
Out[3]: (1, 'spam', 4.0, 'U', 5, 6)
In [4]: T[1]
Out[4]: 'spam'
del T[2]
In [1]: T = (1, 'spam', 4.0, 'U')
In [2]: len(T)
Out[2]: 4
In [3]: T + (5,6)
Out[3]: (1, 'spam', 4.0, 'U', 5, 6)
In [4]: T[1]
Out[4]: 'spam'
In [5]: del T[2]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-9678804ef101> in <module>()
----> 1 del T[2]
TypeError: 'tuple' object doesn't support item deletion
{'food': 'spam, 'color': 'pink', 'quantity': 4}
In [1]: D = {'food': 'spam', 'color': 'pink', 'quantity': 4}
D['food']
In [1]: D = {'food': 'spam', 'color': 'pink', 'quantity': 4}
In [2]: D['food']
Out[2]: 'spam'
D['quantity'] += 1
In [1]: D = {'food': 'spam', 'color': 'pink', 'quantity': 4}
In [2]: D['food']
Out[2]: 'spam'
In [3]: D['quantity'] += 1
In [4]: D
Out[4]: {'color': 'pink', 'food': 'spam', 'quantity': 5}
if conditon_1
do something
elif conditon_2:
do something else
else:
do thing number 3
>>> spam = input(“2 letter word for (and rhymes with) you & me: “) >>> guido = [spam, ‘eggs’]
>>> if guido[1] == ‘eggs’:
guido[1] = ‘are the’ >>> x = guido
>>> sausage = input(“Like a time of day, but begins with ‘k’: “)
>>> y = sausage + ‘s’
>>> bacon = input(“Fill in the blank in this children’s game: Simon ____: “) >>> z = [‘who’, bacon[0:-1]]
>>> print( x, y, z, “Ni!”)
Back to main website: