Autosave: 2023-02-14 09:16:11
This commit is contained in:
parent
ab1203f3ec
commit
4fc1257d30
17 changed files with 642 additions and 33 deletions
17
Programming_Languages/Python/BBC_Course_Notes.md
Normal file
17
Programming_Languages/Python/BBC_Course_Notes.md
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
# BBC Python Course notes
|
||||||
|
|
||||||
|
## TODO:
|
||||||
|
|
||||||
|
## Numbers
|
||||||
|
|
||||||
|
## Control flow
|
||||||
|
|
||||||
|
### Conditionals
|
||||||
|
|
||||||
|
### While loops
|
||||||
|
|
||||||
|
### For loops
|
||||||
|
|
||||||
|
Add example of slightly odd ternary structure
|
||||||
|
|
||||||
|
While, when we don't know how long
|
|
@ -1,33 +0,0 @@
|
||||||
---
|
|
||||||
title: Python data-types
|
|
||||||
categories:
|
|
||||||
- Programming Languages
|
|
||||||
tags: [python, data-types]
|
|
||||||
---
|
|
||||||
|
|
||||||
# Python datatypes
|
|
||||||
|
|
||||||
The core data-types are as follows:
|
|
||||||
|
|
||||||
- str
|
|
||||||
- bool
|
|
||||||
- float
|
|
||||||
- double
|
|
||||||
- ...
|
|
||||||
|
|
||||||
## Converting data-types
|
|
||||||
|
|
||||||
For every data-type there is a corresponding converter method, e.g:
|
|
||||||
|
|
||||||
```python
|
|
||||||
a_string_int = "32"
|
|
||||||
as_int = int(a_string_int)
|
|
||||||
# 32
|
|
||||||
a_float_int = "32.2"
|
|
||||||
as_float = float(a_float_int)
|
|
||||||
# 32.2
|
|
||||||
|
|
||||||
a_bool = "true"
|
|
||||||
as_bool = bool(a_bool)
|
|
||||||
# True
|
|
||||||
```
|
|
59
Programming_Languages/Python/Concepts/Python_data_types.md
Normal file
59
Programming_Languages/Python/Concepts/Python_data_types.md
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
---
|
||||||
|
title: Python data-types
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python data-types
|
||||||
|
|
||||||
|
- Python is dynamically typed rather than untyped. It updates the types on the fly as you are writing your code.
|
||||||
|
- Type-hints in the editor like `-> str` mean "at the moment it is a string". It doesn't mean you can't redefine the value as something else.
|
||||||
|
- Each data type in Python inherits off of a built-in class, similar to prototypes in JS
|
||||||
|
|
||||||
|
The core data-types are as follows:
|
||||||
|
|
||||||
|
- str
|
||||||
|
- bool
|
||||||
|
- float
|
||||||
|
- double
|
||||||
|
|
||||||
|
We can identify types using the built-in `type()` function:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Integer number
|
||||||
|
my_variable = 422
|
||||||
|
print(my_variable)
|
||||||
|
print(type(my_variable))
|
||||||
|
# <class 'int'>
|
||||||
|
|
||||||
|
# String type
|
||||||
|
my_variable = 'Natalia'
|
||||||
|
print(my_variable)
|
||||||
|
print(type(my_variable))
|
||||||
|
# <class 'str'>
|
||||||
|
|
||||||
|
# Boolean type
|
||||||
|
my_variable = True
|
||||||
|
print(my_variable)
|
||||||
|
print(type(my_variable))
|
||||||
|
# <class 'bool'>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Converting data-types
|
||||||
|
|
||||||
|
For every data-type there is a corresponding converter method, e.g:
|
||||||
|
|
||||||
|
```python
|
||||||
|
a_string = '32'
|
||||||
|
print(f'a_string {a_string} is {type(a_string)}')
|
||||||
|
an_int = int(a_string)
|
||||||
|
|
||||||
|
print(f'an_int {a_string} is {type(an_int)}')
|
||||||
|
a_float = float(a_string)
|
||||||
|
|
||||||
|
print(f'a_float {a_string} is {type(a_float)}')
|
||||||
|
another_string = str(42)
|
||||||
|
|
||||||
|
print(f'another_string {a_string} is {type(another_string)}')
|
||||||
|
```
|
15
Programming_Languages/Python/Concepts/Python_execution.md
Normal file
15
Programming_Languages/Python/Concepts/Python_execution.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python execution
|
||||||
|
|
||||||
|
For immediately executable scripts, we have to have a Python shebang at the top:
|
||||||
|
|
||||||
|
```
|
||||||
|
#! /usr/local/bin/python3
|
||||||
|
```
|
||||||
|
|
||||||
|
With programs we can just run the `main` file with `python main.py`.
|
|
@ -0,0 +1,25 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Package management
|
||||||
|
|
||||||
|
- It is better to use `conda` (the package manager that comes with `anaconda`), since this makes it easier to work with conflicting package libraries (a bit like a package lock).
|
||||||
|
|
||||||
|
- The alternative is the native `pip` but you have to create virtual environments (`venv`) to manage packages at different versions.
|
||||||
|
|
||||||
|
It works a bit like this:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
To make use of virtual environments in `pip` you have to create the virtual environment before installing anything:
|
||||||
|
|
||||||
|
```
|
||||||
|
python3 -m venv venv3
|
||||||
|
source venv3/bin/activate
|
||||||
|
pip [library_name]
|
||||||
|
```
|
||||||
|
|
||||||
|
- pypi.org > is package registry like NPM
|
|
@ -0,0 +1,72 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Conditional statements in Python
|
||||||
|
|
||||||
|
## Basic example
|
||||||
|
|
||||||
|
```python
|
||||||
|
input_string = input('Please input a number: ')
|
||||||
|
|
||||||
|
if input_string.isnumeric():
|
||||||
|
print('The number is accepted')
|
||||||
|
else:
|
||||||
|
print('The input is invalid')
|
||||||
|
|
||||||
|
# 5
|
||||||
|
# The number is accepted
|
||||||
|
|
||||||
|
# Using an and in the condition
|
||||||
|
print('-' * 25)
|
||||||
|
age = 15
|
||||||
|
status = None
|
||||||
|
if age > 12 and age < 20:
|
||||||
|
status = 'teenager'
|
||||||
|
else:
|
||||||
|
status = 'not teenager'
|
||||||
|
print(status)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Else if
|
||||||
|
|
||||||
|
```python
|
||||||
|
savings = float(input("Enter how much you have in savings: "))
|
||||||
|
|
||||||
|
if savings == 0:
|
||||||
|
print("Sorry no savings")
|
||||||
|
elif savings < 500:
|
||||||
|
print('Well done')
|
||||||
|
elif savings < 1000:
|
||||||
|
print('That is a tidy sum')
|
||||||
|
elif savings < 10000:
|
||||||
|
print('Welcome Sir!')
|
||||||
|
else:
|
||||||
|
print('Thank you')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Nested conditions
|
||||||
|
|
||||||
|
```python
|
||||||
|
snowing = True
|
||||||
|
temp = -1
|
||||||
|
if temp < 0:
|
||||||
|
print('It is freezing')
|
||||||
|
if snowing:
|
||||||
|
print('Put on boots')
|
||||||
|
print('Time for Hot Chocolate')
|
||||||
|
print('Bye')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ternaries/ shorthand conditionals
|
||||||
|
|
||||||
|
```python
|
||||||
|
status = 'teenager' if age > 12 and age < 20 else 'not teenager'
|
||||||
|
print(status)
|
||||||
|
|
||||||
|
num = int(input('Enter a simple number: '))
|
||||||
|
result = -1 if num < 0 else 1
|
||||||
|
print('Result is ', result)
|
||||||
|
```
|
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-structures]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Dictionaries in Python
|
||||||
|
|
||||||
|
Dictionaries are basically the Python equivalent of objects in JS.
|
||||||
|
|
||||||
|
Dictionaries:
|
||||||
|
|
||||||
|
- Are ordered (in contrast to JS)
|
||||||
|
- Are mutable
|
||||||
|
- Are indexed by a key which references a value
|
||||||
|
- Can be increased/decreased in length by adding/removing new members.
|
17
Programming_Languages/Python/Syntax/Lists_in_Python.md
Normal file
17
Programming_Languages/Python/Syntax/Lists_in_Python.md
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-structures]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Lists in Python
|
||||||
|
|
||||||
|
Lists are the equivalent of a simple array in JavaScript.
|
||||||
|
|
||||||
|
Lists have the following properties:
|
||||||
|
|
||||||
|
- They are **ordered**
|
||||||
|
- They are **mutable** and can be modified
|
||||||
|
- They **allow duplicate** members
|
||||||
|
- They are **indexed**
|
||||||
|
- You can increase/decrease their length by adding/removing new members
|
126
Programming_Languages/Python/Syntax/Loops_in_Python.md
Normal file
126
Programming_Languages/Python/Syntax/Loops_in_Python.md
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Loops in Python
|
||||||
|
|
||||||
|
## While
|
||||||
|
|
||||||
|
```python
|
||||||
|
count = 0
|
||||||
|
print('Starting')
|
||||||
|
while count < 10:
|
||||||
|
print(count, '', end='')
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
print() # not part of the while loop
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
"""
|
||||||
|
Starting
|
||||||
|
0 1 2 3 4 5 6 7 8 9
|
||||||
|
Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
> There are no `do while` loops in Python
|
||||||
|
|
||||||
|
## For
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Loop over a set of values in a range
|
||||||
|
print('Print out values in a range')
|
||||||
|
for i in range(0, 10):
|
||||||
|
print(i, ' ', end='')
|
||||||
|
print()
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
"""
|
||||||
|
Print out values in a range
|
||||||
|
0 1 2 3 4 5 6 7 8 9
|
||||||
|
Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Now use values in a range but increment by 2
|
||||||
|
print('Print out values in a range with an increment of 2')
|
||||||
|
for i in range(0, 10, 2):
|
||||||
|
print(i, ' ', end='')
|
||||||
|
print()
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
"""
|
||||||
|
Print out values in a range with an increment of 2
|
||||||
|
0 2 4 6 8
|
||||||
|
Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Now use an 'anonymous' loop variable
|
||||||
|
for _ in range(0, 10):
|
||||||
|
print('.', end='')
|
||||||
|
print()
|
||||||
|
|
||||||
|
print('-' * 25)
|
||||||
|
|
||||||
|
# Illustrates use of break statement
|
||||||
|
print('Only print code if all iterations completed')
|
||||||
|
num = int(input('Enter a number to check for: '))
|
||||||
|
for i in range(0, 6):
|
||||||
|
if i == num:
|
||||||
|
break
|
||||||
|
print(i, ' ', end='')
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
"""
|
||||||
|
Only print code if all iterations completed
|
||||||
|
Enter a number to check for: 7
|
||||||
|
0 1 2 3 4 5 Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Illustrates use of continue statement
|
||||||
|
for i in range(0, 10):
|
||||||
|
print(i, ' ', end='')
|
||||||
|
if i % 2 == 1:
|
||||||
|
continue
|
||||||
|
print('hey its an even number')
|
||||||
|
print('we love even numbers')
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
"""
|
||||||
|
0 hey its an even number
|
||||||
|
we love even numbers
|
||||||
|
1 2 hey its an even number
|
||||||
|
we love even numbers
|
||||||
|
3 4 hey its an even number
|
||||||
|
we love even numbers
|
||||||
|
5 6 hey its an even number
|
||||||
|
we love even numbers
|
||||||
|
7 8 hey its an even number
|
||||||
|
we love even numbers
|
||||||
|
9 Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Illustrates use of else statement with a for loop
|
||||||
|
print('Only print code if all iterations completed')
|
||||||
|
num = int(input('Enter a number to check for: '))
|
||||||
|
for i in range(0, 6):
|
||||||
|
if i == num:
|
||||||
|
break
|
||||||
|
print(i, ' ', end='')
|
||||||
|
else:
|
||||||
|
print()
|
||||||
|
print('All iterations successful')
|
||||||
|
print('Done')
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Only print code if all iterations completed
|
||||||
|
Enter a number to check for: 6
|
||||||
|
0 1 2 3 4 5
|
||||||
|
All iterations successful
|
||||||
|
Done
|
||||||
|
"""
|
||||||
|
|
||||||
|
```
|
|
@ -0,0 +1,44 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Match statements in Python
|
||||||
|
|
||||||
|
> A `match` statement is the equivalent of a switch or case statement in Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
command = input("What are you doing next? ")
|
||||||
|
|
||||||
|
match command:
|
||||||
|
case "quit":
|
||||||
|
print("Goodbye!")
|
||||||
|
case "look":
|
||||||
|
print("Looking out")
|
||||||
|
case "up" | "down":
|
||||||
|
print("up or down")
|
||||||
|
case _:
|
||||||
|
print("The default")
|
||||||
|
|
||||||
|
"""
|
||||||
|
What are you doing next? up
|
||||||
|
up or down
|
||||||
|
"""
|
||||||
|
|
||||||
|
match command.split():
|
||||||
|
case ["go", "left"]:
|
||||||
|
print("go left")
|
||||||
|
case ["go", ("fast" | "slow")]:
|
||||||
|
print("go fast or slow")
|
||||||
|
|
||||||
|
point = (3, 3)
|
||||||
|
match point:
|
||||||
|
case (x, y) if x == y:
|
||||||
|
print(f"The point is located on the diagonal Y=X at {x}.")
|
||||||
|
case (x, y):
|
||||||
|
print(f"Point is not on the diagonal.")
|
||||||
|
"""
|
||||||
|
The point is located on the diagonal Y=X at 3.
|
||||||
|
"""
|
||||||
|
```
|
43
Programming_Languages/Python/Syntax/None_in_Python.md
Normal file
43
Programming_Languages/Python/Syntax/None_in_Python.md
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# None in Python
|
||||||
|
|
||||||
|
`None` is not `null`, it is closer to `undefined` in JS. If you define a variable as `None`, the variable exists, it is just not yet defined.
|
||||||
|
|
||||||
|
Using `None` is a pattern similar to using `let` in JS to name a variable and definine it later on.
|
||||||
|
|
||||||
|
```python
|
||||||
|
temperature = None
|
||||||
|
```
|
||||||
|
|
||||||
|
If we logged `temperature` it would give us `None` rather than a null pointer error.
|
||||||
|
|
||||||
|
With None we can use `is None` and `is not None`, special predicates for working with `None` only. This is a akin to using `if (x !== undefined)` in TypeScript
|
||||||
|
|
||||||
|
```python
|
||||||
|
winner = None
|
||||||
|
print('winner:', winner)
|
||||||
|
# winner: None
|
||||||
|
print('winner is None:', winner is None)
|
||||||
|
# winner is None: True
|
||||||
|
print('winner is not None:', winner is not None)
|
||||||
|
# winner is not None: False
|
||||||
|
print(type(winner))
|
||||||
|
# <class 'NoneType'>
|
||||||
|
# Now set winner to be True
|
||||||
|
print('Set winner to True')
|
||||||
|
# Set winner to True
|
||||||
|
winner = True
|
||||||
|
print('winner:', winner)
|
||||||
|
# winner: True
|
||||||
|
print('winner is None:', winner is None)
|
||||||
|
# winner is None: False
|
||||||
|
print('winner is not None:', winner is not None)
|
||||||
|
# winner is not None: True
|
||||||
|
print(type(winner))
|
||||||
|
# <class 'bool'>
|
||||||
|
```
|
34
Programming_Languages/Python/Syntax/Numbers_in_Python.md
Normal file
34
Programming_Languages/Python/Syntax/Numbers_in_Python.md
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Numbers in Python
|
||||||
|
|
||||||
|
## Distinguishing `int` and `float`
|
||||||
|
|
||||||
|
- In Python we have floats and integers and we can coerce one into the other
|
||||||
|
- A `//` as an operator means float division. This obviously provides greater precision than int division `/`.
|
||||||
|
- There is no increment (`++`) or decrement (`--`) operator in Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Integers and floats
|
||||||
|
count = 1
|
||||||
|
print(count)
|
||||||
|
# 1
|
||||||
|
print(type(count))
|
||||||
|
# <class 'int'>
|
||||||
|
|
||||||
|
exchange_rate = 1.83
|
||||||
|
print(exchange_rate)
|
||||||
|
# 1.83
|
||||||
|
print(type(exchange_rate))
|
||||||
|
# <class 'float'>
|
||||||
|
|
||||||
|
print(float(count))
|
||||||
|
# 1.0
|
||||||
|
|
||||||
|
print(int(exchange_rate))
|
||||||
|
# 1
|
||||||
|
```
|
12
Programming_Languages/Python/Syntax/Sets_in_Python.md
Normal file
12
Programming_Languages/Python/Syntax/Sets_in_Python.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-structures]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Sets in Python
|
||||||
|
|
||||||
|
- They are **unordered**
|
||||||
|
- You can increase/decrease their length by adding/removing new members
|
||||||
|
- They **do not allow duplicate members**
|
||||||
|
- **Can only hold immutable objects**
|
78
Programming_Languages/Python/Syntax/Strings_in_Python.md
Normal file
78
Programming_Languages/Python/Syntax/Strings_in_Python.md
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Strings in Python
|
||||||
|
|
||||||
|
> Generally, anything that changes a string will be a method on the `str` class, rather than a built-in function like `len()`, as such it will use dot notation
|
||||||
|
|
||||||
|
- Strings are **immutable**: string operations produce a new string.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Working with Strings
|
||||||
|
|
||||||
|
my_variable = 'Bob'
|
||||||
|
print(my_variable)
|
||||||
|
# Bob
|
||||||
|
|
||||||
|
my_variable = "Eloise"
|
||||||
|
print(my_variable)
|
||||||
|
# Eloise
|
||||||
|
|
||||||
|
# A multi line string
|
||||||
|
my_variable = """
|
||||||
|
Hello
|
||||||
|
World
|
||||||
|
"""
|
||||||
|
print(my_variable)
|
||||||
|
"""
|
||||||
|
Hello
|
||||||
|
World
|
||||||
|
"""
|
||||||
|
|
||||||
|
my_string = 'Hello World'
|
||||||
|
print(len(my_string))
|
||||||
|
# 11
|
||||||
|
|
||||||
|
string_1 = 'Good'
|
||||||
|
string_2 = " day"
|
||||||
|
string_3 = string_1 + string_2
|
||||||
|
print(string_3)
|
||||||
|
# Good day
|
||||||
|
|
||||||
|
msg = 'Hello Lloyd you are ' + str(21)
|
||||||
|
print(msg)
|
||||||
|
# Hello Lloyd you are 21
|
||||||
|
|
||||||
|
# Range of String operations
|
||||||
|
msg = 'Hello World'
|
||||||
|
print(msg.replace("Hello", "Goodbye"))
|
||||||
|
# Goodbye World
|
||||||
|
|
||||||
|
print('Edward Alan Rawlings'.find('Alan'))
|
||||||
|
# 7
|
||||||
|
|
||||||
|
print('Edward John Rawlings'.find('Alan'))
|
||||||
|
# -1
|
||||||
|
|
||||||
|
print('James' == 'James') # prints True
|
||||||
|
print('James' != 'John') # prints True
|
||||||
|
|
||||||
|
print("msg.startswith('H')", msg.startswith('H'))
|
||||||
|
# msg.startswith('H') True
|
||||||
|
|
||||||
|
print("msg.endswith('d')", msg.endswith('d'))
|
||||||
|
# msg.endswith('d') TRUE
|
||||||
|
|
||||||
|
print('some_string.upper()', msg.upper())
|
||||||
|
# some_string.upper() HELLO WORLD
|
||||||
|
|
||||||
|
print('sub string: ', 'Hello-World'[1:5])
|
||||||
|
# sub string: ello
|
||||||
|
|
||||||
|
# String interpolation
|
||||||
|
user_age = input("Please enter your age: ")
|
||||||
|
print(f'You are {user_age}')
|
||||||
|
```
|
71
Programming_Languages/Python/Syntax/Tuples_in_Python.md
Normal file
71
Programming_Languages/Python/Syntax/Tuples_in_Python.md
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, data-structures]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Tuples in Python
|
||||||
|
|
||||||
|
Tuples are one of the main data-structures or containers in Python.
|
||||||
|
|
||||||
|
Tuples have the following properties:
|
||||||
|
|
||||||
|
- They are **ordered**
|
||||||
|
- They have a **fixed size**
|
||||||
|
- They are **immutable** and cannot be modified
|
||||||
|
- **Allow duplicate** members
|
||||||
|
- They are **indexed**
|
||||||
|
|
||||||
|
```python
|
||||||
|
tup1 = (1, 3, 5, 7)
|
||||||
|
print('tup1[0]:\t', tup1[0])
|
||||||
|
print('tup1[1]:\t', tup1[1])
|
||||||
|
print('tup1[2]:\t', tup1[2])
|
||||||
|
print('tup1[3]:\t', tup1[3])
|
||||||
|
|
||||||
|
"""
|
||||||
|
tup1[0]: 1
|
||||||
|
tup1[1]: 3
|
||||||
|
tup1[2]: 5
|
||||||
|
tup1[3]: 7
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Slicing
|
||||||
|
|
||||||
|
print('tup1[1:3]:\t', tup1[1:3])
|
||||||
|
print('tup1[:3]:\t', tup1[:3])
|
||||||
|
print('tup1[1:]:\t', tup1[1:])
|
||||||
|
print('tup1[::-1]:\t', tup1[::-1])
|
||||||
|
|
||||||
|
"""
|
||||||
|
tup1[1:3]: (3, 5)
|
||||||
|
tup1[:3]: (1, 3, 5)
|
||||||
|
tup1[1:]: (3, 5, 7)
|
||||||
|
tup1[::-1]: (7, 5, 3, 1)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
print('len(tup1):\t', len(tup1))
|
||||||
|
# len(tup1): 4
|
||||||
|
|
||||||
|
tup2 = (1, 'John', True, -23.45)
|
||||||
|
print(tup2)
|
||||||
|
# (1, 'John', True, -23.45)
|
||||||
|
|
||||||
|
|
||||||
|
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
|
||||||
|
for x in tup3:
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
print(tup3.count('apple'))
|
||||||
|
print(tup3.index('pear'))
|
||||||
|
|
||||||
|
if 'orange' in tup3:
|
||||||
|
print('orange is in the Tuple')
|
||||||
|
|
||||||
|
tuple1 = (1, 3, 5, 7)
|
||||||
|
tuple2 = ('John', 'Denise', 'Phoebe', 'Adam')
|
||||||
|
tuple3 = (42, tuple1, tuple2, 5.5)
|
||||||
|
print(tuple3)
|
||||||
|
|
||||||
|
```
|
BIN
_img/Screenshot 2023-02-13 at 10.43.17.png
Normal file
BIN
_img/Screenshot 2023-02-13 at 10.43.17.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 452 KiB |
|
@ -1,5 +1,18 @@
|
||||||
# Learning Topic Log
|
# Learning Topic Log
|
||||||
|
|
||||||
|
## Python
|
||||||
|
|
||||||
|
- Get PEP8 working in VSCode as linter and add to execute on save. (These are conventions not syntactic enforcements, style)
|
||||||
|
|
||||||
|
- Is there a thing like prettier to enforce double quotes over single for instance?
|
||||||
|
|
||||||
|
- Research: How do I make sure I am using `conda` and not the default install in VSCode ? PyCharm makes it easy to select environment.
|
||||||
|
|
||||||
|
- Research: best practice for separating projects into `conda` environments like npm
|
||||||
|
|
||||||
|
- Read-up more on types: what does it mean for Python to be dynamically typed. What is type-hinting really?
|
||||||
|
- Use provided pdfs and John's books
|
||||||
|
|
||||||
## Bash
|
## Bash
|
||||||
|
|
||||||
- Best way to run a command in a script - is it to `echo` it?
|
- Best way to run a command in a script - is it to `echo` it?
|
||||||
|
|
Loading…
Add table
Reference in a new issue