Python is a modern interpreted programming language that can be used to build programs using a procedural or object-oriented paradigm. It provides many built-in features and has simple and easy to learn syntax. In this article, I’ll introduce you to a complete Python course.
This Python Course is complete coverage of the Python programming language to get into data analysis and machine learning.
Also, Read – Machine Learning Full Course for free.
1. Python Course: The Basics of Python
A programming language is defined by its syntax and semantics, which vary from language to language. But all languages require the use of specific instructions to express algorithms as a program in the given language.
The Python language consists of many different instructions, including those for sequential, selection, and repeat constructs. In this section, let’s take a look at the basic syntax used in building simple sequential instructions.
Identifiers are used to name things in a programming language. In Python, identifiers are case sensitive and can be any length, but they can only contain letters, numbers, or the underscore, and they can’t start with a number. Some identifiers are reserved and cannot be used by the programmer. Here is a list of Python reserved words:
and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield False None True
Variables
Computers manipulate and process data. In Python, all data is stored in objects, which are created from various class definitions. Objects can store one or more values. But regardless of the size, we must have some means to access the object after it is created.
A variable is a named storage location that associates a name with an object. Variables do not store data themselves, but instead, store references to the objects that contain the data. A reference is simply the memory address of the location where the object is stored.
Variables in Python are created the first time an object reference is assigned. Note the following assignment guidelines:
name = "John Smith"
idNum = 42
avg = 3.45
Code language: JavaScript (javascript)
which creates three variables and assigns references to each of the given literal values.
User Interaction
User interaction is very common in computer programs. In GUI (Graphical User Interface) based programs, interaction is handled through widgets or controls displayed in a windowed environment. In text-based programs, user input comes from the keyboard and the output is written to the terminal.
In Python, there is only one function to extract user data via keyboard. The input () function waits for the user to type a sequence of characters followed by the Enter key.
name = input( "What is your name? " )
Code language: JavaScript (javascript)
The input () function can only extract strings. But what if we need to extract an integer or a real value? In this case, the numeric value will be extracted as a string of digits which must be converted to a numeric value using the appropriate numeric type constructor. In the following example:
userInput = input( "What is your gpa? " )
gpa = float( userInput )
Code language: JavaScript (javascript)
we extract a floating-point value from the user by passing the input string to the float constructor, which converts the representation as a numeric string to an actual value. This code segment can be written by nesting the two function calls:
gpa = float( input("What is your gpa?"))
Code language: JavaScript (javascript)
Standard Output
In Python, as you have seen in several examples, the print () function is used to display information about the terminal. When the following is executed:
print( "Hello World!!" )
Code language: PHP (php)
the string is displayed in the terminal followed by a line break, which moves the cursor to the next line. The print () function can only output strings, but Python will implicitly convert all built-in types to strings using the str () constructor. For example, when the following code segment is executed:
avg = (exam1 + exam2 + exam3) / 3.0
print( "Your average is" )
print( avg )
Code language: PHP (php)
the floating point value stored in avg will be converted to a string and displayed on the terminal. The result is:
Your average is 85.732
Selection Constructs
Select statements allow you to choose whether to execute an instruction or a block of instructions based on the result of a logical expression. Python provides several forms of the if construct for making decisions and selecting certain statements to execute.
The if construct can take several forms. The if-then form is used to select a statement or a block of statements if and only if a condition is met. Consider the following example:
if value < 0 :
print( "The value is negative." )
Code language: PHP (php)
where the print () function is executed if the value is negative; otherwise, the instruction is ignored.
2. Python Course: The If-Else Statement
The if-else form of the if construct is used to execute one block of statements if a condition is true and another block of statements if the condition is false. This form is identified by the inclusion of an else clause. In the following example:
if value < 0 :
print( "The value is negative." )
else:
print( "The value is positive." )
Code language: PHP (php)
a value is tested to determine whether it is negative or positive and an appropriate message is displayed.
3. Python Course: Loops In Python
Loops can generally be classified as number controlled or event controlled. In a number-controlled loop, the body of the loop is executed a given number of times based on known values before the loop is executed for the first time. With event-controlled loops, the body is executed until an event occurs. The number of iterations cannot be determined until the program is executed.
While Loops
The while loop is a general loop construction that can be used for many types of loops. Consider the loop controlled by the following number, which adds up the first 100 integers by repeatedly executing the body of the loop 100 times:
theSum = 0
i = 1
while i <=100:
theSum += i
i += 1
print("The Sum", theSum)
Code language: PHP (php)
The while loop, which is a compound statement, consists of two parts: a condition and a loop body. The body of the loop contains one or more instructions that are executed for each iteration of the loop.
The number of iterations is determined by the condition, which is constructed using a logical expression. The body of the while loop is executed as long as the condition is true.
The For Loop
The for loop in Python is part of the built-in iteration mechanism used to iterate through the individual elements stored in a sequence or collection. The individual elements of the given collection are assigned, one by one, to the loop variable.
After each assignment, the body of the loop is executed where the loop variable is commonly used to perform an action on each element. The body of the loop will be executed once for each element in the collection. Consider the following code segment:
thestring = "John Smith"
count = 0
for letter in thestring:
if letter >= "A" and letter <= "Z":
count += 1
print("The string contained %d uppercase characters" %count)
Code language: PHP (php)
which cycles through the individual characters of the string “John Smith”, one at a time, to count the number of uppercase letters in the string. Python’s for statement is equivalent to the for each construct found in other languages.
4. Python Course: Collections
Lists
A Python list is a built-in type of collection that stores an ordered sequence of object references in which individual items are accessible by index. A list can be created and populated using a comma separated sequence in square brackets. For example, the first statement in the code segment:
gradeList = [ 85, 90, 87, 65, 91 ]
listA = [] # two empty lists
listB = list()
Code language: PHP (php)
List items are referenced using the same bracketed index notation used with strings. As with strings, items are numbered in sequential order, with the first item having an index zero. Negative indices can also be used to reference items from the end.
print( 'First item = ', gradeList[0] )
print( 'Last item = ', gradeList[-1] )
Code language: PHP (php)
The elements of a list can also be accessed by browsing the list using the iteration mechanism:
for element in gradeList :
print( element )
Code language: PHP (php)
Tuples
The tuple is another type of built-in collection for creating an ordered sequence of objects. Tuples are just like lists, except that they are immutable. A tuple is created using a pair of parentheses instead of brackets.
t = ( 0, 2, 4 ) # 3 element tuple
a = ( 2, ) # 1 element tuple
b = ( 'abc', 1, 4.5, 5 ) # 4 element mixed tuple
Code language: PHP (php)
Dictionaries
The Python dictionary is a built-in class that stores a collection of entries where each entry contains a key and a corresponding value, sometimes referred to as a payload. A dictionary can be created and populated using a sequence of keys separated by commas: pairs of values listed in curly braces or empty dictionaries can be created using empty braces or the dict () constructor:
states = { 'MS' : 'Mississippi', 'VA' : 'Virginia',
'AL' : 'Alabama', 'DC' : 'Washington' }
classLevel = { 0 : 'Freshman', 1 : 'Sophomore',
2 : 'Junior', 3: 'Senior' }
emptyA = { }
emptyB = dict()
Code language: JavaScript (javascript)
5. Python Course: Functions in Python
A Python function contains a header and a body. The function header is specified using the def keyword while the function body is given as a block of statements. The header consists of a name by which the function can be called and a list of formal parameters separated by commas in parentheses. Consider the following code segment:
def sumrange(first, last):
total = 0
i = first
while i <= last:
total = total + i
i = i + 1
return total
Code language: JavaScript (javascript)
which defines the sumrange() function, which requires two arguments. The function adds the range of values specified by the first and last numeric arguments and returns the result. A user-defined function is called like any other function in Python, as shown in the following code segment:
thesum = sumrange(1, 100)
print(("The sum of first 100 integers is", thesum))
Code language: PHP (php)
6. Python Full Course: Classes
Python not only offers itself as a popular scripting language but also supports the object-oriented programming paradigm. Classes describe data and provide methods for manipulating that data, all encompassed under a single object. Additionally, classes allow abstraction by separating concrete implementation details from abstract representations of data.
class Person(object):
species = "Homo Sapiens"
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def rename(self, renamed):
self.name = renamed
print("Now My Name is", self.name)
Now let’s make some instances:
aman = Person("Aman")
aman.rename("Aman Kharwal")
Code language: JavaScript (javascript)
So with this Python, you can get started with data analysis and then get into the path of Machine Learning. I hope you liked this article on Python full course. Feel free to ask your valuable questions in the comments section below.