DEV Community

loading...
Cover image for Python Basics, Python 101.

Python Basics, Python 101.

Iankips
・4 min read

Python is an interpreted high-level general-purpose programming language. It was created in the late 1980's by Guido Van Rossum.
It supports object-oriented programming as well as procedural programming. Python can be used various computing fields such as, but not limited to, data analysis, machine learning, software development, web-development.

Features.

1. Ease of coding.

Python is a very easy to learn language compared to other programming languages such as Java, C etc. This is because it has a simple syntax.

2. Portable.

This means that when python code is shared, it works the same way it was meant to.

3. Object Oriented.

This aids in breaking down of complex problems in such a way that they can be coded and solved to obtain solutions.

4. Free and open-source.

Python is freely available it can be downloaded, its code is also publicly available.

Getting Started with Python.

To get started with python programming, you will need to download the latest version of python for your operating system. Head over to,python.org and download python3.
You will also need an IDE. There are so many of them such as vs code. Download one and you will be set to start coding. You can get all the information about setting up on their website.

First program.

Let's get started. First, create a folder to save python files. Launch the IDE and open the folder previously created. Create a new python file, say hello.py(python files have a .py extension at the end), enter the following code and save the file.

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

Run the program and observe the output.
The print() is a built-in function that outputs messages on the screen when the code executes.

Now that you have a feel of python let's dive into the basics.

Keywords.

These are reserved words used by python interpreter to understand the program. The cannot be used to name program entities such as functions, variables, classes etc.
Examples of such keywords are; bool, import, True among others.

Identifiers.

These are nothing but names assigned to variables, classes or functions. Can be a combination of uppercase or lowercase letters, numbers or an underscore.

Statements.

This is an instruction that the python interpreter can execute.
An example is the IF statement.

Comments.

These additional information by describing what the code entails. They are not executed by the interpreter. They are of two types, single line and multiline. Single line comments are preceded by a # symbol. Multi-line comments are enclosed in between triple comments.

#Single-line comment.
'''
Multi-line comment
'''
Enter fullscreen mode Exit fullscreen mode

Variables.

Variables are reserved memory location that store values. Unlike other programming languages, python does not have a command for creating variables they are created the moment they are assigned values.
There are rules to be followed while creating variables. Just to mention a few;

  1. A variable cannot start with a number.
  2. Must start with a letter or an underscore.
  3. Variable names are case sensitive i.e. Name is different from name
num = 45
Enter fullscreen mode Exit fullscreen mode

num in the above code is the variable that stores the value 45.

Data types in python.

Number.

This datatype holds numeric characters. They are of three types.
INTERGER: Deals with whole numbers.
FLOAT: Deals with numbers with decimal points. Can be positive or negative.
COMPLEX: Specified with real parts and imaginary parts. Defined in the form a + bj where, a is the real part and j is the imaginary part.
Let's take a look at them.

a = 20  #Interger.
b = 243.567  # Float.
c = 4 + 2j  #Complex.

Enter fullscreen mode Exit fullscreen mode

String.

This is a sequence of characters, be it a digit, letter, white spaces or special symbols, enclosed between single quotes, double quotes or triple quotes. Strings are immutable, this means that once they are created they cannot be changed.

name = "Python"
age = '22'
clause = '''Python is an interesting language which is easy to learn. One with various applications.'''
Enter fullscreen mode Exit fullscreen mode

Some operations associated with strings are;

Concatination.

Allows us to join two strings together using the + operator.

fname = 'John'
lname = 'Doe'
name = fname + ' ' + lname
print(name) # Prints John Doe.
Enter fullscreen mode Exit fullscreen mode

The empty space between the single quotes is used to put space between the two strings.

Repetions.

Allows the repetition of a string using the * operator.

name = "Python"
print(name*2) # prints python twice.
Enter fullscreen mode Exit fullscreen mode
Membership.

This operator is used to check whether a given character is contained is the string or its absent. There are two operators used in and not in. The return value is Boolean true or false.

state = "Coding challenge."
'coding' in state # Returns True.
'python' in state # Returns False.
Enter fullscreen mode Exit fullscreen mode

List.

This is a collection of elements of different datatypes. Lists are mutable, this means that they can be changed even after they have been created. Elements in a list are enclosed between square brackets.

a_list = ['John',34,45.6,"Mary"]
Enter fullscreen mode Exit fullscreen mode

Dictionary.

This is used to store data in key value pairs which are enclosed in curly brackets. Each element is separated by a comma.

details = {"Name": "John", "Age": "22", "Course":"Python programming"}
#Here, Name is the key while the elemnt passed to it, John, is the value.
Enter fullscreen mode Exit fullscreen mode

Type conversion.

This function converts one data type to another.
int() function converts to integer.
float() function converts to a decimal number.

x = 45.78
print(int(x)) # prints 45
Enter fullscreen mode Exit fullscreen mode

To check the type of a variable use the type() function.

Functions.

This is a reusable piece of code that performs a specific task. In python, user-defined functions are created using the def keyword. Follows the following syntax,

def hello():
    print('Hello')

#This function prints hello.
Enter fullscreen mode Exit fullscreen mode

In between the brackets is where parameters can be passed. Parameters are variable listed inside the parenthesis. Arguments are the actual values passed to the parameters.

def add(num):
    total = num + 4
    print(total)
add(5)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)