DEV Community

Cover image for Python 101: The Ultimate Python Tutorial For Beginners
pank-guru
pank-guru

Posted on

Python 101: The Ultimate Python Tutorial For Beginners

Python Fundamentals 1

Programming as a concept is one of the most fascinating concepts in computer science. The concept of being able to make a program that can ease your workload by simply writing a script that automates your work. One of the most fascinating languages in programming in python and bash. Both languages are used in scripting; Python is also used for software development, web development, and data analytics among many other applications. Python is an object-oriented programming language it's slightly similar to Java, another programming language.
Python was invented in the late 1980s by Guido van Rossum it was created to simplify the already complex programming languages that existed before. Python's name is derived from the British comedy group Monty Python, whom Python creator Guido van Rossum enjoyed while developing the language. Python has developed a lot from the first time it was created, recently python3 came into play after some years of using python2, and there were changes to some of the functions in python2 and some improvements.

Why Should I Study Python?

Most people ask why I should study python as the first programming language. The answer to that question might be because python is a cross-platform meaning that it can be used in different devices and operating systems such as Windows, Linux, Mac, and Raspberry Pi just to mention a few. Secondly is because python has a simple syntax that allows the programmer to write his or her programs with fewer lines than some other programming languages an example will be if you wanted to write a program that says hello world it would only take one line

print("Hello World!" 
Enter fullscreen mode Exit fullscreen mode

unlike other programming languages that may take more than three lines. Another reason is that python runs on an interpreter system, meaning that the written code can be executed as soon as it's written. This means prototyping can be very quick.
Python is very strict with indentation; indentation refers to the space at the beginning of a code line. When you do not indent your code after using a conditional statement python will always throw errors since it does not understand the code.

Variable in Python

Variables are memory locations in programming they hold values that one declares in their code such as

age = int(10)
Enter fullscreen mode Exit fullscreen mode

In this example, we have created a memory location and named its age and we have set the memory location to store integer type of data only, and then we have stored the value 10 into our memory location. Variables do not need to be declared with any particular type, and they can even change type after they have been set. Variables do not only take integer data types, you can also declare a variable with string values, characters, floats, doubles and Boolean, etc.
Variables are case sensitive thus

a = 5 
Enter fullscreen mode Exit fullscreen mode

is different from

A=5
Enter fullscreen mode Exit fullscreen mode

thus one should be careful when declaring his or her variables. String variables can be declared with single or double quotes i.e

name = " Python"
Enter fullscreen mode Exit fullscreen mode

is the same as

name='Python'
Enter fullscreen mode Exit fullscreen mode

Variables have their own rules also known as the conventions of declaring variables, they include:

  • A variable name must begin with a letter or the underscore character
  • A variable cannot begin with a number
  • A variable can only contain alphanumeric characters and underscores(A-z,0-9, and _)
  • Variable names are case-sensitive (bot, Bot, and BOT are three different variables)

Output Variables

Output variables: in python, the print()functions are usually used to output variables an example is:

X = "what's your name?"
Print(x)
Enter fullscreen mode Exit fullscreen mode

If the above example is compiled the output will be: what's your name? One can also print multiple variables by separating them with commas an example can be; print(x,y,z). Apart from outputting multiple variables, one can also concatenate variables together to form a sentence. An example of concatenating variables:

 x = "Hey "
 y = "am "
 z = "Python"
print(x+y+z)
print(x,y,z)
Enter fullscreen mode Exit fullscreen mode

output: Hey am Python
NB: You cannot concatenate a string with integer values in case you do it you will be met with a TypeError: unsupported operand type(s) for +: 'int' and 'str'
The best practice for outputting multiple variables in the print() function is to separate them with commas, which even support different data types:

Global variables

Global variables: this is a variable that is declared outside a function. Global variables can be used by anyone, both inside of functions and outside.
In a normal case when you create a variable within a function, that variable is usually local, and can only be used within that function. To create a global variable inside a function, one can use the global keyword. Also, the global keyword can be used inside a function if you want to change a global variable inside a function.

# Creating a variable outside a function and using it within the function
opinion = "Python is amazing"

def myopinion():
  print(f"How is Python:{opinion}")

myopinion()
Enter fullscreen mode Exit fullscreen mode
# creating a global variable inside a function
def greetings():
  global h
  h = "Hae 2"

greetings()

print(f"Hae:{h}")
Enter fullscreen mode Exit fullscreen mode

Comments

Comments are used in programming to give more information about what a section of the code does; they are also used to make the code more readable. Comments are not executed when compiling your code since they are not part of the code so the compiler assumes the comments. Comments in Python start with #, anything is written after the hash (#) python will ignore it since it's a comment.
Example of how to use comments

#This is a comment
#The line below prints Hello World!
print("Hello World!)
Enter fullscreen mode Exit fullscreen mode

Something to note is that comments are not only used for explaining the code, they can also be used in preventing python from executing code.

#print("Hello World")
Enter fullscreen mode Exit fullscreen mode

The above code will be ignored by python since it has been commented out.
The # can only be used in commenting single line if you want to comment on multiple lines you will have to use multi-line comments. Since python does not have syntax for multi-line comments we use docstrings to achieve multi-line commenting, this is because python ignores string literals that are not assigned to a variable. You can achieve this by using triple double quotes ("the lines of code you want to comment go here "") or single quotes three times.
Example

""""
This is a program 
That is written in 
More than a single line
"""
print("Hello Python") 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)