Python is a programming language. Like other languages, it gives us a way to communicate ideas. In the case of a programming language, these ideas are “commands” that people use to communicate with a computer! In this course, we shall cover the fundamentals of the Python programming language.
Overview
This is a complete beginner Python course and it covers the fundamental constructs of the Python programming language. In this course, we shall cover the basics of the Python syntax and also attempt some challenges that will help us grasp the concepts.
In this module, we cover common language constructs like comments, functions, variables, error handling, etc.
Python supports different types of arithmetic operations that can be performed on literal numbers, variables, or some combination.
The plus-equals operator += provides a convenient way to add value to an existing variable and assign the new value back to the same variable.
A variable is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type.
A modulo calculation returns the remainder of a division between the first and second number.
An integer is a number that can be written without a fractional part (no decimal). An integer can be a positive number, a negative number or the number 0 so long as there is no decimal portion.
Python supports the joining (concatenation) of strings together using the + operator. The + operator is also used for mathematical addition operations.
The Python interpreter will report errors present in your code.
A ZeroDivisionError is reported by the Python interpreter when it detects a division operation is being performed and the denominator (bottom number) is 0.
A string is a sequence of characters (letters, numbers, whitespace or punctuation) enclosed by quotation marks. It can be enclosed using either the double quotation mark " or the single quotation mark '.
A SyntaxError is reported by the Python interpreter when some portion of the code is incorrect.
A NameError is reported by the Python interpreter when it detects a variable that is unknown.
Python variables can be assigned to different types of data. One supported data type is the floating-point number. A floating-point number is a value that contains a decimal portion. It can be used to represent numbers that have fractional quantities.
The print() function is used to output text, numbers, or other printable information to the console.
Sometimes functions require input to provide data for their code. This input is defined using parameters.
Python functions can have multiple parameters.
Some tasks need to be performed multiple times within a program.
Python uses indentation to identify blocks of code.
Python uses simple syntax to use, invoke, or call a preexisting function.
Python functions can be defined with named arguments which may have default values provided.
Python functions are able to return multiple values using one return statement.
In Python, a variable defined inside a function is called a local variable.
A variable that is defined outside of a function is called a global variable.
Function parameters behave identically to a function’s local variables. They are initialized with the values passed into the function when it was called. Like local variables, parameters cannot be referenced from outside the scope of the function.
The Python elif statement allows for continued checks to be performed after an initial if statement. An elif statement differs from the else statement because another expression is provided to be checked, just as with the initial if statement.
A try and except block can be used to handle error in code block. Code which may raise an error can be written in the try block.
The Python or operator combines two Boolean expressions and evaluates to True if at least one of the expressions returns True. Otherwise, if both expressions are False, then the entire expression evaluates to False.
The equal operator, ==, is used to compare two values, variables or expressions to determine if they are the same.
If the values being compared are the same, the operator returns True, otherwise, it returns False.
The Python not equals operator, !=, is used to compare two values, variables or expressions to determine if they are NOT the same. If they are NOT the same, the operator returns True. If they are the same, then it returns False.
The Python if statement is used to determine the execution of code based on the evaluation of a Boolean expression.
The Python else statement provides alternate code to execute if the expression in an if statement evaluates to False.
The Python and operator performs a Boolean comparison between two Boolean values, variables, or expressions. If both sides of the operator evaluate to True then the and operator returns True.
Booleans are a data type in Python, much like integers, floats, and strings. However, booleans only have two values:
The Python Boolean not operator is used in a Boolean expression in order to evaluate the expression to its inverse value.