Search⌘ K
AI Features

Overview of Python

Explore fundamental Python programming concepts such as defining and using functions, handling variables and data types, applying basic arithmetic, and controlling code flow with conditional statements. This lesson provides hands-on coding challenges to reinforce your understanding of Python data representation and logic.

Functions

A function is a code block that takes in an input value or an argument and performs an action on it. Figure 1 shows a function that takes an argument, increments it by one, and returns it. In Python, there are two types of functions:

  • Built-in functions are already defined in Python libraries, and we can call them directly.
Figure 1. Functions that increment the given value.

The most common built-in function is the print function. It displays the input value on the console.

Python 3.5
print(5)
  • User-defined functions are functions that we define ourselves in our program and then call them wherever we want.

The user defined function called increment is shown in the code snippet below. This function takes one argument, increments it by one and returns the resultant value.

Python 3.5
def increment(n):
return n+1
result = increment(5)
print(result)

Basic arithmetic operators

You can add two numbers and use the print function to visualize the output.

Python 3.5
print(5 + 5)

Likewise, you can use the four basic operators:

  • add (+)
  • subtract (-)
  • multiply (*)
  • divide (/)

and use the print function to visualize the output.

The order of ...