Python Basis, A Beginners Guide to Python

Esther Vaati
4 min readMay 5, 2022

An introduction to Python Basics: Part 1

Photo by Bermix Studio on Unsplash

Python is one of the top and most Popular Programming languages. It is extensively used in Machine learning and data science.

Did you know that the popular streaming site Youtube is built entirely in Python using Django?. Other popular companies using Python include:

  • Instagram
  • Dropbox
  • Pinterest
  • Mozilla
  • Disqus, e.t.c

Your First Python File

To write python code, the file will have a.py extension, for example, hello.py.
It’s now your turn to write your first Python file using your favorite ide. IDE ( integrated development environment ) is a software application used to build applications. . Open your IDE and create a blank file and name it hello.py. Add the following code to it.

greeting = "Long time no see"
print(greeting)

To run the Python program, issue the following statement on the terminal.

python3 hello.py

You should be in the directory where the hello.py file is located. You should see the following result printed on the terminal if all goes well.

Long time no see

The Print Statement

The print statement you have used above is how a computer communicates. The syntax for the print statement in Python3 is:

  • print followed by a parenthesis and the messages to be printed inside the parenthesis
print(statement)

Variables

Variables in Python store data; for example, in your file hello.py, greeting is a variable.

When you define a Python variable, you can use it later in a function or any other statement. When writing variables, you should follow the following conventions:

  • variables should not have a space
  • variables cant begin with numbers
  • variables can’t be named after built-in functions or methods

Variables can also be reassigned; for example, changing the value of greeting as follows

greeting  = "How are you "
print(greeting)

If you run the program, the output will be:

How are you

Strings

A string is a sequence of characters. In python, a string is enclosed by single or double quotes. For example

string1 =  'Learning Python is fun'
string2 = "Learning Python is fun"
print(string2)
print(string1)

Both statements are correct; whichever one you choose, ensure that you are consistent throughout your code.

Comments

Comments give more meaning to provide explanations to your code. You can also use to ignore a block of code in Python. To write a comment in Python, start with a hash(#) symbol, and everything in that line will be a comment

# This code says hello in different languagesgreeting_in_french = "Bonjour"
greeting_in_arabic= "Marhaba"
#print(greeting_in_french)
print(greeting_in_arabic)

If you run the code above, the computer will ignore the first print statement because its highlighted as a comment and you will get this output

Marhaba

Integers and Floats

There are three types of numbers supported in Python, namely:

  • integers
  • floats
  • complex numbers

Integers and floats are the most commonly used numbers.

Integers are whole numbers, while floats are numbers with decimal places. 12 is an integer whole 12.34 is afloat

You can perform mathematical calculations with integers as follows.

addition

#adittiona= 10
b=20
print(a+b)

subtraction

print(b-a)

multiplication

print(a*b)

division

print(b/a)

Concatenation

Concatenation is the process of joining two or more elements. For example, you can join two strings as follows

name = "Mike"
print("My name is " + name )

The output will be:

My name is Mike

To join an integer to a string, you should first convert the number to a string using the str() method.

name = "Mike"
age =24
print("My name is " + name + " and am " +str(age) + " years old")

The output will be:

My name is Mike and am 24 years old

user input

It’s not every time you will have your data; sometimes, you need user-generated data. Python provides the input function whose syntax is shown below

variable = input("data")

When you use input, you will prompt the user to enter their data, which is assigned to the variable. Create your first input to ask the user for their name and age.

name = input("What is your name")
age = input("How old are you")
print(name)
print(age)

When you run the code above, you should see a prompt; go ahead and type your name and press Enter on your keyboard. Next, you will see another prompt asking for your age, type it and press Enter. The result will be

What is your name: MICHAEL
How old are you: 25
MICHAEL
25

Functions

Functions allow developers to reusable code; this limits a lot of repetition. The syntax for a function in Python is shown below.

def function_name():
#what the function does

For example, write a function that returns a greeting.

def greeting():
print("Hello, there")

If you run the program above, nothing is printed; this is because functions have to be called, as shown below

greeting()

Functions also take in arguments. Arguments are placed inside the parenthesis. Write a function person with two arguments name and age.

def person(name,age):
print("My name is " + name + " and am " +str(age) + " years old")

Now call the function and pass in actual values as arguments

person("Scarlette",29)

When you run the code, the output will be:

My name is Scarlette and am 29 years old

If you enjoyed reading this, you might relish

--

--

Esther Vaati
Esther Vaati

Written by Esther Vaati

Get practical JavaScript tips and code snippets delivered to your inbox. Join 1000 + developers who write better code. https://practicaljavascript.substack.com/

No responses yet