
Discover how Python uses indentation to define code blocks and control execution under specific conditions, including if, else, while, and for loops, without braces.
Explore how variables hold values, assign values with the assignment operator, and print variable content, using the name example to show overwriting from John to Peter.
How to work with number variables (integers and decimal numbers) in Python.
How to get user input from the console
How to get user input from the console when expecting numerical values
Python Exercise: Calculate The Movie Ticket Prices. How much do we have to pay for the movie tickets, where adults pay £10, children pay half and pensioners pay 30%?
Here we will do:
Write pseudocode for:
Get user input for the length and width of the carpet, convert the input to float, calculate the area size of the carpet, multiply the square meter price with the area to get the total price of the carpet and print all the values
Use the pseudocode to code your Python program.
Simple example of an If-Statement in Python.
One can also do if with an else , so there would be 2 options of code, either will run, depending on a condition.
What if there are multiple options, if and elif and else?
For instance,
if the age of a child is < 5, then the entry fee is 0,
but if the child is between 5 and 11, there is a different entry fee;
if the child is between 11 and 16, there is a different entry fee;
if the age is between 16 and 24, there is a student fee;
and if the age is over 65, there is a pensioner fee.
So this needs more than if and else. Let's see what we could do.
Exercise: Movie tickets
Exercise: Travel fee
# Use Python Calculate a delivery_fee :
# If the distance is < 10 km, then the delivery_fee is 0
# Else if the distance <= 20 km, then the delivery_fee is 20
# Else if the distance <= 30 km, then the delivery_fee is 30
# Else delivery_fee=0 and print we do not deliver
# Display the delivery_fee
Looping in Python: For-loops.
To repeat code, one can use a for-loop or a while-loop.
A loop allows Python to repeat the same code a given few times, so that I do not have to type the same code many times.
For instance, what if I have 15346 employees and want to give them all a bonus of £100? Do I add 100 to 15346 different salaries manually? No! I create a loop that will repeat the same calculation for all employees.
Without a loop, that would have been a lot of typing. With a loop, I type it once, and Python repeats the code for the given number of times.
Exercises: Print "Hooray" 3 times, and throw the dice 3 times. Count to 10. Count to 100. What is the sum of all integers between 1 and 10?
Looping in Python: While-loops.
To repeat code, onc can use a for-loop or a while-loop.
A lopp allows Python to repeat the same code a given number of times.
Why is that? Well, I do not feel like typing the same code many times.
I want to type it once, and Python repeats it many times, keeping the count accurately.
For instance, what if I need to repeat something 2538849 times?
Without a loop, that is a lot of typing.
With a loop, I type it once, and Python repeats the code for the given number of times.
Magic!
Break will simply stop the loop! and leave the rest of the iterations undone.
Continue will allow a loop to jump back to the beginning without doing all the steps in the loop. Like when you run the marathon in 4 10k laps but cheat and run only half of each lap. You take a short cut back to the start.
Loops in Python: Break Example Using the break when getting the correct password
The Python Random Class, Create Random Values
Have fun and code a guessing game!
# Generate a random number between 1 and 7
# create an array with 7 movie names, one for each day
# ask the end user to guess the number, they should guess what the generated random number is
# convert the number to integer
# if they guess the correct number, print they can win a ticket to the movie at that index
# use the generated randon number as the index
# else print Better luck next time
Have fun and win Millions!
# Generate a random number between 0 and 1, multiply it with 1Million.
# Generate a random number between 1 and 10
# ask the end user to guess the number between 1 and 10
# they should guess what the generated random number is
# convert the number to integer
# Loop 3 times but stop as soon as they have guessed the correct number
# If they guessed the correct number, print they win the lottery and print the generated Millions
# else print "Better luck next time"
How many characters are in your text variable? e.g., myVariable = "abc", the length is 3. myVariable = "abc def", the length is 7 (the space counts as 1 character).
Slicing a text variable to work with a substring
Explore slicing a text variable in Python to obtain substrings, using zero-based indices and exclusive end positions, with practical examples of starting positions and extracted substrings.
Changing the case construction of text values, e.g. abc to ABC etc
Count the s's in Mississippi which will return the interger value 4. Where is the fisrt s, at index position 2, and the index and find functions both will give you the position of a string within the other.
Replace Dumb with Cool in Dumb and Dumber to get Cool and Cooler.
Does a name start with S as in Smith or ends with ly, like in Molly
Changing the case construction of text values. You need to come back to this lesson after you have viewed the section on Lists.
Python Data Structures: Lists, Sets, Tuples, Dictionaries
Demonstrate dictionaries with key-value pairs and retrieve values by key, while comparing list access by index and len to count items.
What is a tuple, and how do I use it?
What is a set, and how do I use it?
Sometimes you would like to know if there is data in a list. The the len function will return 0 if the list is empty.
Learn to loop a Python list with a traditional index-based for loop and an enhanced for loop, printing each movie such as Bambi, Rambo, Shrek, and Cinderella.
Compare looping a Python list with a conventional for loop using range(len(li)) to get indices and values, with an enhanced for loop for value in li that omits indices.
Learn how to find a value in a Python list using in and not in, plus index-based and enhanced list for loops, with 'Rambo' in a movies list as example.
Python Lists: Insert a new value into a list, and append a new value
Update a value in a list
Now that we have a list, what can I do with a list: append, insert, remove, index, sort and reverse
What is a data dictionary, compared to lists
Learn how to access and print dictionary keys, values, and items in Python, and loop through them to display key-value pairs.
Add a new key and value to a dictionary, such as school: 'ABC school', and verify changes with length and print. Remove a key to see dictionary updates.
Demonstrate how to remove all dictionary values in Python using the clear function, showing before-and-after prints and how the for loop yields no output once cleared.
Explore how to create and access nested dictionaries in Python by examining employee records, using len to count top-level keys, printing dictionaries, and retrieving inner values like John and Mary.
Learn how to use len to count top-level keys in a nested dictionary and count inner fields such as name, age, gender, and married for each employee.
Explore how to access and iterate over keys, values, and items in dictionaries, including nested dictionaries, and print them with separators to display key-value pairs.
Explore nested dictionaries and nested loops by iterating over an outer dictionary and an inner dictionary’s items to print keys and values.
Learn to print nested dictionaries by looping through employees and their attributes, drill into the hobbies dictionary when present, and display name, age, gender, married.
Explore looping nested dictionaries by checking if a value is a dictionary, iterating over nested items, and printing simple values, including handling hobbies dictionaries.
Learn to add values to a nested dictionary by inserting new inner dictionaries for employees, verify length becomes four, and access inner values to retrieve names John, Maury, and Peter.
Explore how to add values to a nested dictionary and access nested values using multiple keys, such as adding a hobby and retrieving Peter's hobbies from a nested structure.
Explore aggregates in dictionaries, including max, min, and sum, with nested dictionaries. See how to get the maximum age, total sum, and calculated average.
Add and delete key value pairs in a Python dictionary by manipulating employee records, then add new fields like country and salary, and sum all salaries to verify totals.
See how to dump a Python dictionary to a json file and load it back using json.dump, json.load, and proper file paths with open in write and read modes.
What is try and except? This is an introduction.
Learning more about the exact reason why I get an error: e and repr(e)
Try and except: Working with different errors. Dealing in different ways with different types of errors
Master Python: A Very Simple Tutorial
You know what it feels like when a video carries on too long about something really simple.
Well, here there is none of that.
Python is a very simple and easy programming language.
This tutorial makes the simple stuff even more simple with very easy and short videos. It is meant to be quick access to the information without any fuss.
Although complex topics are made easy by clear, short, direct explanations. this course is no means just for beginners. Python is covered complrehensively and both beginners, intermediate and advanced users will find tutorials on topics to enhance their knowledge and Python Skills .
Learn Python fwith easy videos!
Python Fundamentals: Grasp the core concepts of Python programming quickly and efficiently.
Build Your Own Projects: Begin immediately implementing your newfound knowledge by developing Python scripts and applications, putting theory into practice. This hands-on approach not only reinforces learning but also cultivates practical skills essential for building real-world projects and enhancing your programming proficiency.
What You'll Learn:
Python Basics: Gain a solid grasp of fundamental concepts like variables, strings, comments, and conditional statements (if statements), ensuring you navigate them with confidence and clarity in your Python journey.
Python Fundamentals: Master loops and if statements.
Python RANDOM Class: Generate Random Numbers
Python Data Structures: Lists, Sets, Tuples
Python and CSV Files: Learn the essentials of file handling, including reading and writing CSV files.
Python Dictionaries and JSON files
Python Database Connectivity: Access a database from using Python code
OOP Concepts: Explore functions, classes, and objects to elevate your Python expertise and tackle more complex programming tasks effectively.
Data Analytics with Dataframes: Create dataframes , import and explore data in dataframes, reduce data by slicing. querying data to find data, and reduce the rows to work with, clean data, restructucture dataframes, combine data from different sources, and more
Whether you’re a complete beginner, just brushing up on the basics, or intermediate user, or brushing up on advanced topics, ,this tutorial will make Python enjoyable and straightforward. Start now and see how quickly you can become a Python pro!