This tutorial will introduce you to the basic concepts Python required for the course. After reading the tutorial, you will be able to understand and write basic Python programs, and explore Python and understand the course very well.

# Comments

A comment in Python starts with the hash: #, and goes until the end of the line.

# This is a comment

# Variables

  • In Python, variables are a storage placeholder for texts and numbers.
  • It must have a name so that you are able to find it again.
  • The variable is always assigned with the equal sign, followed by the value of the variable.
#  This code assigns variable a to Number 5
a = 5
#  This will assign b to string "Hello"
b = "Hello"

# Variable Types

Python has five standard data types :

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

In the course, we'll mostly use just numbers and strings.

#Number
sample_number = 10

# String are enclosed inside double or single quotes.
sample_string = "This is a string"
sample_string_2 = "456"

# Lists are used to store multiple items in a single variable. Values are indexed and are changeable
sample_list = [10,20,0,"Python",30.4]

# A tuple is a collection which is ordered and unchangeable.
sample_tuple = ('Tuple item',2,4)

# A set is a collection which is both unordered and unindexed.
sample_set = {1,2,3,4,5}

# Dictionaries are used to store data values in key:value pairs.

# A dictionary is a collection which is unordered, changeable and does not allow duplicates.

# Dictionaries are written with curly brackets, and have keys and values:
sample_dictionary =  {"key1": "value1" , "Name": "John", "Age": 26}

# Type Casting

The process of converting the value of one data type (integer, string, float, etc.) to another data type explicitly is called type casting.

Consider the following:

num_1 = 123 # Type : Number
num_2 = "456" # Type : String

If you want to add num_1 and num_2 in Python, you can't do that because Python cannot convert string to a number implicitly. So, we have to explicitly convert num_2 to an integer type and proceed to addition.

num_1 = 123
num_2 = "456"

num_2 = int(num_2) # This will convert string "456" to number 456

# Now we can add num_1 and num_2
print(num_1 + num_2) # 579 would be printed

# Printing Strings and Variables

We use the print() function for printing any output.

# This program will print Hello World on the terminal

print("Hello, World!")

# Printing Variables:

There are mainly two ways of printing variables:

  • Using comma operator:

    # For example lets take a = 5, b = 10
    a = 5
    b = 10
    # This is one way of printing variable
    print("The value of variable a is", a , "and value of b is", b)
    
    

    The output of the above code would be:

    The value of variable a is 5 and value of b is 10
    

    The comma operator adds space after the string as well.

  • String Interpolation:

    Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f. This new way of formatting strings is powerful and easy to use.

    name = 'World'
    program = 'Python'
    
    # Just use f before the quotation marks
    print(f"Hello {name}! This is {program}")
    

    The output of the above program would be:

    Hello World! This is Python
    

    Simple and easy, right? That's why we'll be using this method in the course.

# Python Booleans

Booleans represent one of two values: True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer:

print(10 > 9) # This will print True
print(10 == 9) # False
print(10 < 9) # False

# Boolean function:

The bool() function allows you to evaluate any value, and give you True or False in return.

bool("Hello") # True
bool(15) # True
bool(None) # False

# Conditional Statements

The if...else statement is used if you want perform different action (run different code) on different condition. For example:

num = -1

if num > 0:
    print("Positive number")
elif num == 0: #elif is short for else if
    print("Zero")
else:
    print("Negative number")

# Output: Negative number

# Error Handling

When an error or an exception occurs, Python normally stops its execution and generates an error message.

Example:

print(x)
# This statement will generate error because variable x was not defined before it was printed.

In Python, these errors and exceptions can be handled using a try statement. The operation which can raise an exception is placed inside the try block. The code that handles exception is written in the except block.

try:
  print(x)
except:
  print("An exception occurred") 

Here, as the try block raises an error (x is not defined), the except block will be executed and An exception occurred would be printed on the terminal.

# Conclusion

These are all the things that you'll need for the course. Make sure you understand these well so that there isn't any knowledge gap in the future videos.