DEV Community

Cover image for Introduction to Modern Python
Elkanah Malonza
Elkanah Malonza

Posted on


Introduction to Modern Python

Python is an interpreted high-level general-purpose programming language. Its a general purpose language meaning it has a range of potential uses like:

  • Web and Internet Development

  • Database Access

  • Desktop GUIs

  • Scientific & Numeric

  • Education

  • Network Programming Software & Game Development

The advantage of using python programming language compared to other language are:

  1. Friendly and easy to Learn - Python is an easily readable and simple to understand language for developers.

  2. Free - Python is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use.

  3. Highly Compatible - python plays well with others and can runs everywhere.

  4. Lots of libraries - Python's standard library and the community-contributed modules allow for endless possibilities (This is the main reason why I wake, eat, drink, sleep and dream Python everyday). Example NumPy and TensorFlow Python Library is used for everything from data visualization, machine learning, data science, natural language processing, and complex data analysis.

  5. Wide Application - we have already mentioned how python is a general purpose language with a wide range of application from web development to scientific applications.

In this article we are going to cover the basic concepts to get you started with python. We will cover:

  1. Python Expressions
  2. Data Types
  3. Variables
  5. Inbuilt Functions
  6. Putting all together - Your First Python Program

1. Python Expressions

A sequence of operands and operators, likex + y - 5, is called an expression. 1 + 2 is called an expression, which is the most basic kind of programming instruction in the language. Expressions can always evaluate down to a single value.

Python supports the following operators for combining data objects into expressions:

   **  -   Exponent
   %   -   Reminder
   //  -   Integer division/floored quotient ( 22 // 8 = 2)
   /   -   Division (22/8 = 2.75)
   *   -   Multiplication
   +   -   Addition
   -   -   Subtraction
Enter fullscreen mode Exit fullscreen mode

The above operators are arranged in their order of operations or precedence which is similar to the order in mathematics. You can use parentheses to override the usual precedence. Examples of expressions:

5 * 2 
5 ** 2 
5 % 2 
5 / 2 
5 // 2 
Enter fullscreen mode Exit fullscreen mode

2. Data Types

A data type is a category for ­values, and every value belongs to exactly one data type. Python data types are:

  • Integers - This are values that are whole numbers. Examples: -2 , -1 , 0 , 1 , 2 , 3 , 4 , 5
  • Floating-point numbers - this are numbers with a decimal point. Example: 3.142, 1.25 , -1.0 , ‐ -0.5 , 0.0 , 0.5 , 1.0 , 1.25
  • Strings - This are text values, defined by surrounding your string in single quote. Examples: 'a' , 'aa' , 'aaa' , 'Hello!' , '11 cats'

3. Variables

Variables are used to store information to be referenced and manipulated in a computer program. You’ll store values in variables with an assignment statement which consists of a variable name, an equal sign (assignment operator) and the value to be stored. In buffer = 54, then a variable named buffer will have the integer value 54 stored in it. Example:

data = 53 # initializing a variable
buffer = 62
buffer = 37 # overwriting the variable
data = buffer + data # they can be used with expression
name = 'malonza' # a variable holding strings
Enter fullscreen mode Exit fullscreen mode

A variable name can be of any length as long as they follow the following rules:

  1. It can be only one word.
  2. It can use only letters, numbers, and the underscore ( _ ) character.
  3. It can’t begin with a number.
a = 23 # valid

 _name = 'john' # valid

zoo3 = 'lazy' # valid
Enter fullscreen mode Exit fullscreen mode

4. Python Comments
Python ignores comments when the code is executed, and you can use them to write notes or remind yourself what the code is trying to do.

  • Any text for the rest of the line following a hash mark ( # ) is part of a comment.
# Program to add two numbers
x, y = 2, 3
Enter fullscreen mode Exit fullscreen mode
  • Using 3 single quotes at the beginning and end of a block of text make the text a comment.This style of commenting your code can transverse multiple line unlike using the hash(#).
A Random text with
multiple lines to show you how to add
comment in multiple line of code
Enter fullscreen mode Exit fullscreen mode

5. Built-in Function
A function is a block of organized, reusable code that is used to perform a single, related action.
A built-in function is a function that is provided as part of a high-level language (python in this instance) and can be executed by a simple reference with specification of arguments.
The print() Function
The print() function displays the string value inside the parentheses on the screen. Example:

print("Hello World!")
name = 'Malonza '
Enter fullscreen mode Exit fullscreen mode

The input() Function
The input() function waits for the user to type some text on the keyboard and press enter.

name = input()
Enter fullscreen mode Exit fullscreen mode

The len() Function
You can pass the len() function a string value (or a variable containing astring), and the function evaluates to the integer value of the number of characters in that string.

names = "Elkanah Malonza"
Enter fullscreen mode Exit fullscreen mode

The str(), int(), and float() Functions

The str() , int() , and float() functions will evaluate to the string, integer, and floating-point forms of the value you pass, respectively.

Enter fullscreen mode Exit fullscreen mode

6. Putting all together - Your First Python Program

Now it’s time to create your first program!

Open a file/text editor such as Notepad or sublime text and type the following into it:

name = input("What is your Name: ")
print("Wow!, You name is ", len(name), "Long.")
dob = int(input("When were you born", name, ": ")) # the int() function to convert the input() value to integer
age = 2019 - dob
print("You are ", age, "years old.")
print("THanks ", name, "for choosing Python3.")
Enter fullscreen mode Exit fullscreen mode

Once you’ve entered your source code, save it as

open a Terminal or command line interface and move to the file location using cd command.

cd [path_to_the_file_location]

To run or execute the file type the following command and press enter:



Top comments (0)