Python Dictionaries for Frequency Count

What will we cover?

We will learn about dictionaries and how to use them for frequency count.

See the full tutorial on Python Dictionaries as well.

Step 1: What is a Dictionary?

A Python dictionary stores a key-value pair in a mutable data structure.

That sounds crazy, but let’s take a look at how you define them and how they can be used.

my_dict = {
    'Key 1': 'Value 1',
    'Key 2': 'Value 2'
}

print(my_dict['Key 1'])
print(my_dict['Key 2'])

This means you can define key-value pairs between the curly-brackets and look up values with keys.

The keys need to be unique, while the values can be arbitrary. Also, the key and values can be of any type and not only as strings like the above example.

Step 2: The magic of Python Dictionaries

Look at this code.

my_dict = {
    'Key 1': 'Value 1',
    'Key 2': 'Value 2'
}

my_dict['Key 3'] = 'Value 3'
my_dict['Key 2'] = 'Value 3'

If you have a WHAT-moment, I am with you. If not, let’s break it down.

First you define the initial dictionary my_dict. Then you add a new key-value pair (my_dict[‘Key 3’] = ‘Value 3’).

Can you do that?

I am glad you asked. And yes, you can just add as many key-value pairs you want. No questions ask, the Python interpreter will take care of it and add it to the dictionary.

Then the line my_dict[‘Key 2’] = ‘Value 3’ re-assigns the key Key 2 to a new value. And yes, you can do that too.

This is why Python dictionaries are so powerful.

Step 3: Using Python Dictionaries for keeping Records

A normal use-case of Python dictionaries it to keep records of data.

Think of a SQL data base or a Excel spreadsheet with rows of data. Each row has column names with data. For an example see this tutorial.

Such a row can be represented inside a program with a Python dictionary, which is convenient for handling it.

car = {
    'Brand': "Lamborghini",
    'Model': "Sián",
    'Year': 2020
}

The above could represent a line of car brand, model and year from a spreadsheet.

Notice that Year is actually a integer.

Step 4: Frequency count with a Python dictionary

Look at this example where we use a for-loop to iterate over a list.

items = ['Pen', 'Scissor', 'Pen', 'Pen', 'Scissor']

count = {}
for item in items:
    count[item] = count.get(item, 0) + 1

Now that is nice. What happens?

Well we first have a Python list of items and we want to count how many of each items there is.

Then we initialize an empty dictionary with the curly brackets {}.

Can we do that? Yes, and that is why we love Python, it is such a joy to work with for the programmer.

Then we iterate over the Python list and start to update our dictionary. See, this is where the real magic happens. We assign to the key item the number of items already counted added one: count[item] = count.get(item, 0) + 1. The beauty is, that get(item, 0) is looking up if the key item exists in the dictionary, if not, it will return 0.

Think about that. Then it will count the number of occurrences of each unique item in the list.

Also, notice, that you count all the items without knowing any of them before you start.

Step 5: Iterate over a Python dictionary

You might be thinking. This is nice, but can I get the result from the dictionary without knowing the keys?

Assume we continue the example from step 4.

for key, value in count.items():
    print(key, value)

Remember, count is a dictionary, then count.items() returns the key-value pairs in a sequence, hence we can loop over them all.

Similarly, you can iterate over the keys.

for key in count.keys():
    print(key, count[value])

Want more?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Caesar Cipher implemented simple in Python

What will we cover?

We will implement the Caesar Cipher in Python using functions and a few things.

For futter preparation than given in this tutorial you can watch this video tutorial.

Step 1: Describing Caesar Cipher

  • The Caesar Cipher is a simple substitution cipher, which is limited to only shift the characters by fixed number.
  • Let’s take an example
    • Imagine you got the message: BRX DUH DZHVRPH. What to make out of it. Makes no sense.
    • But what if we get help from this diagram

Step 2: Implement the encryption function

Let’s implement a helper function encrypt_char(char, key) to encrypt a single character, char, with key.

def encrypt_char(char, key):
    return chr(ord('A') + (ord(char) - ord('A') + key) % 26)

Then we can implement encrypt_message(message, key), which uses the helper function.

def encrypt_message(message, key):
    message = message.upper()
    cipher = ''
    for char in message:
        if char not in ' ,.':
            cipher += encrypt_char(char, key)
        else:
            cipher += char
    return cipher

Let’s try it.

encrypt_message("you are awesome.", 3)

Which results in BRX DUH DZHVRPH.

Step 3: Implement the decryption function

This is quite similar.

def decrypt_char(char, key):
    return chr(ord('A') + (ord(char) - ord('A') + 26 - key) % 26)

def decrypt_message(cipher, key):
    cipher = cipher.upper()
    message = ''
    for char in cipher:
        if char not in ' ,.':
            message += decrypt_char(char, key)
        else:
            message += char
    return message

Let’s try it.

decrypt_message('BRX DUH DZHVRPH.', 3)

Then you get YOU ARE AWESOME.

What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Master For and While Loops in Python to Create a Hangman Game

What will we cover?

We will cover how to loop over a list. Further, we will learn the power of a While loop. Finally, we will create a Hangman Game.

Step 1: Loop over a list or sequence

Looping over a list can be done as follows (see more about lists here).

my_list = ['First', 'Second', 'Third']

for item in my_list:
    print(item)

This can also be done over a string, which is a sequence.

s = "Rune"

for c in s:
    print(c)
    print("Inside loop")
print("Outside the loop")

Where we notice the syntax of how indentation works. What is inside and outside the loop.

Step 2: While loops and how they differ

Notice that for-loops in Python loop over a predetermined length. In the above cases, the length of the list or length of the string.

If you want a loop the is dependent on something inside the loop.

What do I mean. Let’s take an example.

value = 10

while value > 0:
    value = int(input("Value? "))

As you see – this loop is dependent on input in the loop. If you type zero or negative value, it will stop.

Step 3: Description of the Hangman Game

  • The game is as follows.
  • Computer has a list of words.
  • Computer chooses a random word from the list.
  • The player gets 10 wrong guesses (10 turns).
  • The game follows this loop
    • Computer prints the word character by character replacing with underscore those not guessed yet (initial no characters has been guessed).
    • Player guesses a character.
    • If character is not in word, a turn is withdrawn
    • If no turns left, computer wins.
    • If player has guessed all characters, player wins

Step 4: Implementing the Hangman Game

Let’s try to implement the Hangman Game

import random

words = ['father', 'enterprise', 'science', 'programming', 'resistance', 'fiction', 'condition', 'reverse', 'computer', 'python']

word = random.choice(words)

turns = 10

guesses = ''

while turns > 0:
    print(f"Turns left: {turns}")
    
    guessed_all = True
    for c in word:
        if c in guesses:
            print(c, end=' ')
        else:
            print('_', end=' ')
            guessed_all = False
    print()
    if guessed_all:
        print("You won")
        break
    
    guess = input("Guess a character: ")
    
    if guess in word:
        guesses += guess
    else:
        turns -= 1
else:
    print("You lost")

Step 5: Want more?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Master Python Lists Basics to Create a Jumbled Game

What will we cover?

In this tutorial we will learn Python Lists basics. This will enable us to create a Jumbled Game.

Step 1: Python Lists and Indexing

A Python list is like a list you know it. And the beauty of Python lists is that they can contain anything.

But let’s get started immediately. You can define a list as follows. This list contains strings, but it could contain any types or objects.

my_list = ['Apple', 'Orange', 'Banana']

You get the length of a list by using len().

len(my_list)

Which will return 3.

A list is indexed from 0 – that is you get the first element as follows.

my_list[0]

The second element.

my_list[1]

And it continues as you can guess.

You can index from the end of a list by negative indexing – the last element is indexed by -1.

my_list[-1]

Then the second last element with.

my_list[-2]

Step 2: Get a random element from a list.

Remember the random library we used?

Well, it can be applied on a list.

import random

my_list = ['Apple', 'Orange', 'Banana']
random_item = random.choice(my_list)
print(random_item)

This will pick a random item from the list.

Step 3: Pick random samples from a sequence like a string

Imagine you want to get random samples from a sequence?

What is a sequence, well it can be a string. A string in Python is a sequence.

Then you can pick random samples from it.

letter_sequence = 'abcdefgh'
samples = random.sample(letter_sequence, len(letter_sequence))

Then samples will be a list of unique elements from letter_sequence. Hence, it is all the letters uniquely represented.

Step 4: The Jumbled Game Explained

The jumbled game can be described as follows.

  • A word jumble is a word puzzle game that presents the player with a bunch of mixed up letters and requires them to unscramble the letters to find the hidden word.
  • The computer will take word and jumble it (mix up the letters).
  • Then the player will guess what the word is
  • An initial word list could be: [‘father’, ‘enterprise’, ‘science’, ‘programming’, ‘resistance’, ‘fiction’, ‘condition’, ‘reverse’, ‘computer’, ‘python’]

Step 5: Implement the Jumbled Game

This is straight forward with our competencies.

import random

words = ['father', 'enterprise', 'science', 'programming', 'resistance', 'fiction', 'condition', 'reverse', 'computer', 'python']
word = random.choice(words) 

jumble = random.sample(word, len(word))
jumble = ''.join(jumble)

print(f"The jumble word is: {jumble}")
guess = input(f"Write your guess: ")

if guess.lower() == word:
    print(f"Corret! The {jumble} is {guess}")
else:
    print(f"Incorrect! The {jumble} is {word}")

Want more?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Rock-Scissor-Paper made Easy with Python – An Introduction to Randomness in Python

What will we cover?

How to use Randomness in Python and create a Rock-Scissor-Paper game.

Step 1: Randomness in Python

To make games interesting you need some unpredictable aspects. This is where randomness is used.

Luckily, Python has a library to make randomness. To simulate rolling a die can be done as follows.

import random

die = random.randint(1, 6)
print(die)

Where randint(1, 6) returns a random integer from 1 to 6, both inclusive.

Step 2: The Rock-Scissor-Paper game

I think most know the game. If not, read the rules on wiki.

Let’s try a game of it.

print("Enter choice \n 1. Rock \n 2. Paper \n 3. Scissor \n")
choice = int(input("Choice: "))
computer_choice = random.randint(1, 3)

if choice == computer_choice:
    print("Draw")
elif choice == 1:
    if computer_choice == 2:
        print("Computer wins, Paper")
    else:
        print("You win, Scissor")
elif choice == 2:
    if computer_choice == 1:
        print("You win, Rock")
    else:
        print("Computer wins, Scissor")
elif choice == 3:
    if computer_choice == 1:
        print("Computer wins, Rock")
    else:
        print("You win, Paper")

Step 3: What is Next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Program Flows in Python – Conditional Statements and Boolean Expressions Controls the Program Flow

What will we cover?

We will cover what a program flow is, how this links to conditional statements and boolean expressions.

Step 1: Let’s recall what boolean expressions are

Boolean expression is the building block of conditional statements. So let’s make a brief recap of it.

Given two variables a and b, we have the following boolean expressions with them.

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

Where depending on the values in a and b the expressions will evaluate to True of False.

Step 2: Conditional statements

Let’s demonstrate a few if-statements here.

Equality between variables.

a = 10
b = 10
if a == b:
    print("a equals b")

Notice, that if a was not equal b, then it would not print the statement. That is, the indented code after the if-statement, is only executed if the boolean expression evaluates to True.

Another example with less-than.

a = 10
b = 8
if a < b:
    print("a is less than b")

Again the same principles applies.

You can also have an else-clause.

a = 12
b = 12
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

And a elif-clause.

a = 13
b = 12
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

And a not-equal statement.

a = 10
b = 10
if a != b:
    print("a does not equal b")

What next?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Type Conversion in Python

What will we cover?

What is type conversion, why is it useful, and how to use type conversion in Python.

Step 1: What is type conversion in Python?

As we learned, variables in Python have types.

Sometimes we need to convert from one type to another. Say, you have an integer variable, but you need the value represented as a string.

That is type conversion. Converting from one type to another.

Notice that type conversion has many names, you might have heard of type casting, type coercion, or type juggling. But they are all about changing the expression of a data type to another.

Step 2: Why do we need type conversion?

Let’s demonstrate an issue.

Consider the following program.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
print(f"You are {2021 - birth_year} old!")

Well, you expect the program to write out your age.

Unfortunately, it will not function.

Why? Because 2021 – birth_year is not valid. 2021 is an integer, while birth_year is a string. But, you cannot subtract a string from an integer.

So what to do?

Type conversion.

Step 3: How to make type conversion in Python

Now we understand the problem. Let’s try to solve it.

Luckily, Python is our friend and has built in functions.

  • int() Converts to an integer.
  • float() Converts to a float.
  • str() Converts to a string.

Let’s try the first one.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
birth_year = int(birth_year)
print(f"You are {2021 - birth_year} old!")

Now it works as expected and the only difference is the statement birth_year = int(birth_year).

Nice and easy, it converts birth_year to an integer (if possible)

The other functions work similarly.

What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Understand Variables in Python and the Main Types of Variables

What will we cover?

We will cover the what a variable is. How you should name the variables, and finally, the main types of variables in Python.

Step 1: What is a Python variable?

A variable can be defined as follows.

  • A variable is a reserved memory location.
  • A variable in a python program gives data to the computer for processing.

When you work with data, you need somewhere to keep it. That is what variable are used for.

A simple example of a variable can be demonstrated as follows.

s = input('What is your name')
print('Hi', s)

Here, s, is a variable. It takes input from the user and stores it there. Then it prints the value of the variable on the next line.

Notice that we used similar variables in our first lesson.

In Python we have the following basic types.

  • Integers
  • Floats
  • Strings
  • Boolean

We will work more with them later – so don’t worry that you do not understand them yet.

Step 2: Naming convention of Variables in Python

A variable name in Python should fulfil the following

  • Use lowercase
  • Separate words with underscore.

A variable name in Python can be

  • A single lowercase letter
  • A lowercase word
  • Or lowercase words separated by underscore.

Examples of variable names

  • x
  • i
  • cars
  • my_cars

Note that other programming languages can have other naming conventions.

You can declare variables as follows.

s = "This is a string"
a = 2
b = 2.4

And get the type of a variable as follows.

type(b)

Step 3: Python type Integer

An integer is a number which is not a fraction; a whole number.

Examples of integers: 

  • -23
  • -5
  • 0
  • 1
  • 78
  • 1,980,350

Examples of non-integers: 

  • 3.14
  • 0.001
  • 9,999.999

Try the following.

a = 2
b = 3

Then you can make arithmetic operations with them.

c = a + b

Now c is an integer with value 5.

Not convinced? Try to get the type as follows.

type(a + b)

Step 4: Python type Float

A float is a numbers with digits on both sides of a decimal point. This is in contrast to the integer data type, which houses an integer or whole number.

Examples of floats:

  • 3.13
  • 9.99
  • 0.0001

Note that a float data type in Python can represent a whole number.

  • 1.0
  • 3.0

An example could be.

a = 1.1
b = 2.3

You can also make arithmetic operations.

c = a + b

Then c will be a float.

Funny enough, if you have a float and add an integer, then you get a float. Let’s try.

d = 1
e = 1.1
f = d + e

Try to take the type of all them to make sure they are the expected types.

type(d), type(e), type(f)

Notice you can comma separate statements like that, and it will output it in a comma separated line.

Step 5: A few useful Math functions

When working with integers and floats it can be handy with a few helpful mathematical functions.

Here we will look at.

  • abs() Returns absolute value of a number
  • pow() Raises a number to a power
  • round() Rounds a floating-point value

The abs() function returns the absolute value of an integer or float.

a = -2
abs(a)

The pow() returns the number to a power.

pow(2, 3)

Notice that this can also be accomplished as follows.

2**3

Finally, round() is very useful function used all the time. It rounds the value of a float.

f = 1.234456778
round(f, 4)

Where the second argument of round is the precision after the decimal point.

Step 6: String in Python

A string in Python is a sequence of characters.

Examples of strings in Python includes the following.

  • “I am a string!”
  • ‘I am another string’

That is, you can both use single and double quoted strings. The standard is to use single-quoted strings, but the double-quoted are used in special cases.

  • “I’m a double quoted string”

Where you see it can have a quote inside the string.

That said, many, me included, use single and double quoted string inconsistently.

s1 = 'I am a string'
s2 = "I am another string"

Given the two strings above you can use addition to concatenate them.

s1 + s2

Yes, that is right. You can add strings together. But you cannot add a string and an integer or float.

Step 7: Print Strings and Integers and Floats with Python

The easiest way to print variables of different types in Python is to comma separate them.

a = 1
b = 1.1
c = "I am a string"
print(a, b, c)

This will work fine, while something like print(a + b + c) will fail.

A great way to print formatted output is as follows.

a = 1
print(f'I am a string with an int here {a + 2} - and more text')

Hence, if you put an f in front of the quoted string, then you can add {} with integer or float statements – see it can be a variable or an arithmetic expression.

Step 8: Boolean variables in Python

If you are new to programming, then boolean expression can be quite difficult to understand. Or actually not. Most people think they get them, but do not understand the full content of the power of boolean expression.

Actually, boolean expressions is what makes programming possible. Boolean expressions take simple scripts that do the exact same every time they are executed to full programs, which behave differently depending on input and context.

So what are boolean values?

  • True or False (case sensitive)

And a boolean expression is something that evaluates to either True or False.

That said, everything in your life is the same.

Say, every morning you look out the window to see if it rains. If it rains you prepare yourself for that. If not, well, you enjoy it not raining.

Some object to this. They say, it is not always easy to say if it rains or not. Well, you can the break it down to, either you bring your umbrella or not.

  • Either it rains or not -> Either you take your umbrella or not

Somehow all your decisions can be translated to boolean expression.

But let’s get started simple.

b = 10 > 5

What? Well, you assign the boolean expression to 10 > 5. If 10 is larger than 5, then it will be True.

Check the value in b, it is True.

How can this be powerful, you might ask?

Well, when we look at conditional flows, you will be able to make powerful programs that can do whatever you want it to.

Step 9: What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Your First Python Lesson – One Step Closer to Mastery

What will we cover?

We will take you from knowing nothing about Python, what to install to get started, how to write your first programs, make a few interactive programs, and learn a few string methods.

Finally, if you still are hooked, guide you to our free 8 hours Python course for beginners, which is a 17 lesson course, including 17 projects and 17 video tutorials you can follow along.

Step 1: Install JuPyter Notebook and Launch your first Notebook

Anaconda install all you need and it is free. That is, it install Python, most of the libraries you need, and a simple interactive environment to develop Python code in, the JuPyter Notebook.

Shortly explained.

  • Anaconda installs all and you can launch JuPyter Notebook from Anaconda Navigator (often just called Anaconda).
  • JuPyter Notebook can navigate through your files and open Notebooks, where you write your code.

How to get started?

  1. Go to Anaconda and download the individual edition (it is free). 
  2. It will install Python and Jupyter notebook. That is all you need to get started and it is all free.
  3. Launch Anaconda Navigator.
  4. In Anaconda launch Jupyter Notebook.
  5. Navigate to a place where you want to save your files.
  6. Create a new Python Notebook with the “new” menu in the right.

Step 2: Writing your first program “Hello, World!”

Click on the first cell and write the following code.

print('Hello, World!')

Press SHIFT-ENTER to execute it.

It should print “Hello, World!” below the cell.

The Hello, World! program is traditionally the first program you write in a language, which demonstrates the syntax.

Python has very little syntax and is easy to use. To compare with other languages see Wikipedia.

Step 3: Input a string from the user

Making programs interactive is fun. The first step is to get the user to type something and let your program do something with that.

s = input("What is your name? ")
print(s)

Look at the code and guess what it does.

Then copy the code into a cell and execute it.

What happened?

It prompted you for something – and you might type some letters, say, your name. Then what will it do? Yes, print your name.

Step 4: Your first interactive program

Actually, you have the tools now to make simple interactive programs.

Let’s try that now.

name = input("What is your name? ")
print("Hello", name.capitalize())
age = input("How old are you? ")
print("You are", age, "years old")

See how simple that is.

Well, you learned a few things here.

if you have a string (like, name), then you can apply capitalize() on it. What does it do?

Well, if you type your name in lowercase, then it will capitalize your name for you. If it already is capitalized, it will do nothing.

Now that is smart.

Step 5: A few methods on strings

Like capitalize() there are a lot of other methods you can apply.

In the beginning you probably worry about remember them all. I got good news for you – you do not need to. You will most likely only use a few and the others you will lookup when you need them.

But a few ones that can be funny to master are the following.

"Rune".upper()
"Rune".lower()
"223".isdecimal()
"Rune".isdecimal()

Try to copy each one into a cell by itself and see what it does. It will most likely make sense to you.

Notice, if you copy them all into one cell in JuPyter Notebook, then it will only give the output of the last statement of the cell. Therefore, try one line at the time in one cell.

Notice, that the “Rune” is a string and the methods could be applied on variables as well.

s = "Rune"
s.upper()

Step 6: Replace something in a string

My favorite and most used string method is replace(.)

Let’s just try it.

name = "Rune"
name.replace('R', 'S')

Put it into a cell and execute.

It will output Sune. It will change all occurrences of R to S.

Notice, that these methods are case sensitive, meaning, that the following code.

name = "Rune"
name.replace('r', 's')

It will output Rune, because there is no lowercase r to replace.

Step 7: What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is the first lesson of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

Check out the first lesson here.

What about the full course?

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE eBook with all the learnings from the lessons.

See the full FREE course page here.

Click and get started on your journey to Python Mastery

How to Learn Python for Data Science

What will we cover?

  • Is Python the correct language to learn for a Data Scientist?
  • How much Python do you need to learn as a Data Scientist?
  • How to learn Python fast?
  • How long does it take to become good at Python?
  • How to get started with Python?

Is Python the correct language to learn for a Data Scientist?

That is a good question to ask yourself. You want to become a Data Scientist, maybe you have some experience, but feel weak in the programming aspect, or maybe you start from scratch.

If I was to start my journey as a Data Scientist one of the questions I would ask myself, is, do I have the tools for it.

R is often high on the scale of programming language and environment to use as a Data Scientist. The language R is designed for effective data handling, operations on arrays and matrices, has data analysis tools, graphical facilities, and well established environment.

That sounds like all we need, so why bother looking further?

In the top there is a battle between two candidates: Python vs R.

Actually, Python is a general purpose language that has a wide aspects of uses, not only Data Scientist. Also, web services, game development, big data backend systems processing high volume data, just to mention a few.

With this description, it looks like R is tailored for Data Science, while Python is used for everything. The choice seems easy – do you want a tool made for the purpose, or something for general purpose?

Funny enough, as it might seem at first, Python has become more popular than R. Why is that?

A few reasons why Python is more popular than R.

  • Python is easy to use and learn.
  • Python has powerfull fast libraries.
  • Python has a huge community and it is easy to get help.
  • Python has easy data handling tools for reading and generating spreadsheets, parquet files, csv files, web scraping, sql databasis, and much more.
  • Python has great Machine Learning libraries developed by giants like Google (tensorflow) and Facebook (PyTorch).
  • Python support graphical data representation with libraries like Matplotlib.
  • Python has SciKit-learn for predictive data analysis.
  • Python has easy to use data representation with NumPy and pandas.

…and the list could go on.

Python is also a great fit when you want to build tailored-made system, which integrate up against any other platform or service, like automatically get data from various sources.

Do I need a Computer Science degree to use Python?

Python is programming and programmers have computer science degrees. Do you need one to become a good Data Scientist?

The short answer is: No.

A Computer Science degrees will enable you to build anything. Let’s try to think of it differently.

Think of transportation – car, busses, bikes, trains, which can move you from A to B. People without a driving license can use busses and trains. All they need is to know how to buy a ticket, understand a schedule to find out to get from A to B. If you get a driver license, then you can driver your own car. Finally, if you are a car mechanics, you can repair and possibly build your own car.

Similarly, a computer science degree will enable you to build cars, busses, trains, and more, which other people can use. A Data Scientist is like a person with a driver license, and you don’t need to be able to repair a car to drive it. That is, you only need to understand and navigate the dashboard in the car.

Data Science is the same., you need to understand the things you use, but you do not need to be able to build them yourself.

But wait! You might object. It is still programming, when I use the things I use.

Yes, but the level of programming is simple and you use the complicated things like you use a car without being a car mechanics.

Feel more comfortable?

How to Learn Python Fast?

Now you are ready and know what you want – how to get there fastest without wasting time.

Maybe one question before that .

Can everybody learn Python? Do you need special skills?

I have so far never met anyone, which could not learn Python to the level of Data Science – and honestly, also for the level of Computer Scientist. It is just a question about dedication and interest to get to the last steps.

But becoming a Data Scientist using Python is not a problem.

The question is more how to learn it fast? The best way to answer that is to look at some of the most common pitfalls that make people learn it slower and some give up on the way.

Pitfall 1: I understand the solution when I see, but why couldn’t I figure it out – am I stupid?

Did you ever learn a new language – a speaking one – like English. If you are non-native English, then you started learning English at once. Remember that?

First you started understanding a few words. Then you started to understand full sentences when people where speaking English, but you could barely express yourself in English yourself. It took time to get there.

Programming is the same – at first you can read and understand the solutions to your problem, it takes time for you to be able to express yourself in programming language.

The feeling you have while trying to solve a programming problem for long time, but not succeeding can be devastating. Then when you see the solution and it looks simple, then you start to feel stupid.

But stop there – this is normal. You learn first to understand code before you can express yourself in code. Just like learning a new speaking language.

We have all been there – and we still get there – just with different more complex problems. It will never end, you will just become comfortable about it and the challenges you face will be more and more complex.

Pitfall 2: Get distracted when it gets tough

When something gets difficult the easy exit is to quit and start something new easier.

Maybe you think, this is too difficult for me – I am not smart enough. This is more fun, so I start this now.

The truth is, that every talented programmer on planet earth has multiple times been stuck at a problem for days – not being able to solve it – if it was a bug or just a difficult problem to solve does not matter. But they have all been struggling with a problem for long time.

This can be quite difficult to deal with as a beginner. You sit with a problem, which does not seem hard and you feel like everyone else can solve it – the logical conclusion is that you are not smart enough, right?

Then you might change to another programming project – and think that is fine, you will still learn programming.

But the truth is, that solving hard problems or finding bugs is not easy. It takes time and you will learn a lot from it. Escaping to another project will not teach you as much as the difficult ones.

The best programmers are the ones that never give up when it gets tough. This is what the highly paid consultant are paid for, solving problems where other give up.

Pitfall 3: Different sources of learning

This is often difficult to understand in the beginning. But there are many styles in programming.

When you know people and been working professionally with them in a development environment for long time, you can actually see who coded it. Their style falls through.

Why does that matter?

In the beginning it does. Because, what most also fail to understand in the beginning is, that you can solve problems in endless ways. There is often no perfect solution for a problem, only different solutions which different tradeoffs.

As a beginner, you want to learn programming and you will not see the differences in styles. But if you starte learning from one person, then another one, then yet another one, then it becomes difficult.

This has never been more relevant in the age where so many people share online learning.

Again, it is like learning English with a specific dialect and different vocabulary. It is difficult in the beginning to distinguish between them, and difficult to see it matters. But in the long run you will speak English optimized for your environment.

Keep focused learning from one source. Do not change from one place to another all the time. Master the basics from one place until you are comfortable about it.

Pitfall 4: Comparing yourself to others

We often compare our learning journeys to others. You need to know if you are doing good or bad, if you need to adjust your approach or not.

This sounds good, right?

You need to keep in touch with reality and not waste time.

This is a major pitfall. You will see solutions to your problems, which are solved more elegant. There will be people that ‘just started’ and are already typing in code like you would never dream of.

This is devastating. Do you not have what it takes?

As hard as it is to accept, that you are not the fastest learner, and you need to work harder than others to reach the same. It is just as hard to realize, that the people you compare yourself with are often the top-of-the-top.

We all have our own journey. Mine is different from yours. I was good at one thing in the beginning, but you are awesome at something I never understood.

Accept that we all have our own journey – there will be times when you feel like the only one not understanding something simple (or at least I did that many times) – but other times when you actually understand something extremely complex.

We often miss these aspects, because we always compare ourselves to the brightest person in our context in any moment. That might be different persons from time to time.

Further, in the days of internet, the environment you compare yourself to is huge.

As you see, this comparison is not fair and will do you no good.

Accept that your journey is yours alone. Comparisons with others do not help you.

How long does it take to become a good Python programmer

I wish there was a simple answer to that. Unfortunately it is not that easy to answer.

First of all, what are your initial expectations and how will they evolve over time. Often people are fine with just some simple skills, but when they learn more they want to master more and it never stops.

It is natural. The problem is, your expectations to feeling successful moves along the way.

Secondly, is the dedication to it. You need to spend time on solving problems.

Experience shows, that either you need to burn for learning programming or you need it to solve you daily challenges.

It sounds like you need to keep motivated. And yes, you do. But the good news is, it is very rewarding and fulfilling to program. You are building something, you are creating something, you are the creator of something amazing. That feeling is awesome.

Does that mean it is just fun all the way from the beginning to end. Not at all, did you read the pitfalls above? Well, if you didn’t, go read them.

What I am saying is, it is a journey that will never end. The journey will sometimes feel bumpy, but the results are rewarding.

The more time you spend, the faster and better results you will get.

But how to keep motivation?

  • Remind yourself daily, that there are pitfall and all the best in the world have been there.
  • Keep it playful – the majority of the time it is joyful to program.
  • Accept it as a learning journey that will never end.

How to get started with Python for Data Science?

On this page there are a lot of resources available to get started with both Python and Data Science.

To help you further there are structured free courses you can follow with everything prepared.

Start Python for FREE

There is a full 8 hours video corse for Python.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE eBook with all the learnings from the lessons.
Get started today with Python for FREE

Start Machine Learning for FREE

Another great free resource is the 10 hours free Machine Learning course.

  • 15 video lessons – which explain Machine Learning concepts, demonstrate models on real data, introduce projects and show a solution (YouTube playlist).
  • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
  • 15 projects – with step guides to help you structure your solutions and solution explained in the end of video lessons (GitHub).
Get started with Machine Learning with Python for FREE