---
# System prepended metadata

title: Python Practical 101

---

# Python Practical 101

## Introduction

The following practical will be used to cover some of the basics of Python, testing your ability to implent simple principles of the language and create a fairly straightforward program. This is the first part of a series of praticals which are going to follow an independent learning approach. The idea is that after working through the practicals you will grasp a fundamental understanding of Python and it's Syntax.

# Lists vs. Sets vs. Tuples vs. Dictionaries

- **Lists** are, essentially, a list of values and are mutable (their values can be changed at runtime) and zero-indexed.
- **Sets** are very similar to lists but do not contain duplicates (if an item is already in the set the set does not change) and can not be indexed into. You can still, however, iterate over them using a for each loop.
- **Tuples** are like lists but immutable (their values can not be changed during runtime) and are zero-indexed.
- **Dictionaries** are similar to real dictionaries where each Key (word) points to a Value (definition). The key and value pairs in a dictionary are not numbered or ordered. You can add, remove, and modify the values in a dictionary.

**Lists**, **sets**, **tuples** and **dictionaries** are all fundamentally similar to each other but each are suited to different scenarios.


# Loops
There are all sorts of ways to either loop through elements of a list of loop through the numbers 1-50. The following are examples of the more common methods.

## Standard for loop
The standard for loop in Python has a few varieties however, they all essentially mean the same thing.
```Python
for i in range(a, b)
```
means "starting at the number *a*, set *i* to the value of *a* and then increase *i* by 1 after each loop until *i* is no longer less then *b*"

If *a* is set to 0 above then it is directly equvilant to:
```Python
for i in range(b)
```
If the range function is only given a single value then *i* will automatically be initially set to 0.

To do list:
for loops
- different types
    - for i in range(len(list))
    - for i in range(0, len(list))
    - for i in range(0, len(list), 2)

for each loops
while loops
break and continue
*advanved* else on a loop: should be called *"no break"*
## Ding, dong the witch is decked
Task: You are currently bored at work and are thinking about playing some of your favourite card games, the only problem is that you don't have a deck of cards to play with. Luckily you know how to program in Python and are able to create one from scratch.



```Python
suits = ["Diamonds", "Hearts", "Spades", "Clubs"]

# target -> [(1, "Diamonds"), (2, "Diamonds"), (3, "Diamonds")]

for suit in suits:
    # Make your deck here
    # Hint: there are 13 cards in each suit
    for i in range(1, 14):
        deck.append((i, suit))
    
```
list/set/dictionary comprehension for beginners

```python



deck = []
for suit in ["D", "H", "S", "C"]:
    for i in range(1, 14):
        deck.append((suit, i))
print(deck)
```

# Practical Ideas/Notes


- Function to deal out a deck to 4 players (players represented by lists)

Then into creating a game:
Players take turns to perform actions.
- Rule 1: Take first card of all players and the player with the highest card wins. Winning player gets a point
    - Rule 1.1 If any highest card numbers are the same, use this order of suits: Diamond > Heart > Club > Spade
- Rule 2: If a card number is the 7, choose randomly swap players hands with each other, do not compare cards this round - not points should be awarded

Console interface

Statistics of games:
- Create a function/functions that will simulate x amounts





New Thing
Centered around creating a black-jack.
First must create basic functions to help:
- Given a "poor quality" deck (explain why), create a function that will create a new "good quality" deck from the poor one
    - Poor deck like so: ['A-Diamonds', 'A-Clubs', 'A-Spades', 'A-Hearts', '2-Diamonds', '2-Clubs', ...]
    - Good deck like so: [(1, 'Diamonds'), (2, 'Diamonds'), (3, 'Diamonds'), (4, 'Diamonds'), ...]
- Function to shuffle a deck
    - Students could just use Python's own random.shuffle function but advise and challenge them to create their own
- Command line interface (TUI) using while loop
- Dictionary for stats - bust counter for cards?
- Maybe thing of one more
- Extensions...