This content originally appeared on DEV Community and was authored by BochaberiDaisy
Python is a popular and fast growing programming language. It was invented by Guido Van Rossum, a Dutch programmer. Programmers fall in love with Python as it increases productivity. Simple and understandable syntax and easy debugging are some of the features of Python that make the language loveable. The main differentiator of Python from other language is the rule on indentation, as we'll come to learn later on here, it is a big deal.
It is used in some of the popular web products such as Youtube and Google.
Features of Python
- It being a high level language makes it is easy to code thus a good beginner-friendly language learn.
- It is an open source language making its source code available to the public.
- It is versatile supporting both functional and object oriented programming.
- It is portable. Python code can be run on different operating systems without the need of changing the code.
- It is easily integrated with other languages such as C, C++, etc.
- It is an interpreted language. Python code is executed line by line at a time. It also doesn't require to be compiled like many other languages
Applications of Python
Web programming
Scripting
Scientific computing
Embedded development
Installation
There are various versions of Python. The most common are Python 3 and Python 2. Python 3 is a newer and better version of Python 2. If you try to run the Python 2 code in Python 3, then you will get the error and vice versa. This is indicates that the syntaxes are different.
Click here to download Python.
Comments
Comments help other readers and your future self understand your code better. It also improves code readability.
# This is a comment
""" This is
a multiline
comment
"""
#Comments are only readable to humans.
print
is a built-in function which displays the logs of your code. The text should be enclosed in single or double quotes
Here comes the first line of code every programmer outputs when they learn a new language, 'Hello World'.
print('Hello World')
print("Hello World")
#This will cause a SyntaxError
print('Hello World")
Indentation
Indentation are the leading whitespaces i.e. spaces and tabs before any statement in python. As it is in other languages such as C# and C++, indentation serves the purpose of code readability. In Python, indentation is mandatory as statements with the same level of indentation are treated as a single code block. It much like how curly braces{}
work in other languages such as C and Java.
To indent you can use the Tab key or use 4 spaces.
Forget to indent a statement and the Python interpreter will throw a IndentationError
. It might seem like a constant bug when you get the errors but once you get used to the concept you'll have gained a good programming practice applicable in other languages as well.
Variables
Variables are the names assigned to a value and are used to refer to the value later in the program. The value assigned to a variable is called a literal.
Initializing a variable in Python doesn't require you to assign the type to the variable as it is in other languages. Assigning a value to a variable is done with the equal =
sign.
There are rules that apply in regards to the characters used in Python variable names. They include;
Only letters, numbers and underscores are allowed.
They can't start with numbers.
Keywords such as
global
can't be used.
user = "Monique"
age = 28
#This results in an error
123name = "Mark"
name@2 = "Marilyn"
Data types
Data types are the classification of data items. Python has a built-in function type
that is used to check for the data type.
Some of the basic data types in Python include:
1. Integers
These are whole numbers that can be positive, negative or zero.
num = 83653
temp = -40
type(num)
type(temp)
Output:
<class 'int'>
<class 'int'>
2. Complex numbers
These specifies real part + (imaginary part) j.
a = 22 + 7j
type(a)
Output:
<class 'complex'>
3. Float
This is a real number with a floating point representation. It is specified with the decimal point.
Divisions or operations done with a float result into floats.
height = 124.34
type(height)
Output:
<class 'float'>
4. String
Strings are sequences of character data. String literals are delimited using either single or double quotes.
type("I am a string.")
Output:
<class 'str'>
Escape sequences in strings are used to interpret a character or sequence of characters within a string differently. This is accomplished by using a backslash () character.
print("It\'s fun learning Python.")
Output:
It's fun learning Python.
5. Boolean
This has two built-in values, True
or False
.
type(True)
type(False)
Output:
<class 'bool'>
<class 'bool'>
6. Lists
Ever heard of an array or a matrix, lists are pretty much the same except it is more efficient. It is created by placing elements inside square brackets [], separated by commas. It can contain items of different types (strings, integers, etc). It can also have another list which is called a nested list.
list = ["father", 34, "mother", 32, "brother", 10]
print(list)
Output:
["father", 34, "mother", 32, "brother", 10]
To access items on a list we can use the index operator []
. Indices start from [0]
and [-1] indicates the end of the list. Negative indices start from the end of the list to the left.
list = ["father", 34, "mother", 32, "brother", 10]
print(list[-3])
print(list[0])
Output:
32
father
To change a list i.e. to add to it or remove from it, we can use built-in functions.
append() - adds items to end of the list.
pop() - removes items from the end of the list.
list = ["father", 34, "mother", 32, "brother", 10]
list.append("sister")
print(list)
list.append(6)
print(list)
list.append(867)
print(list)
list.pop()
print(list)
Output:
['father', 34, 'mother', 32, 'brother', 10, 'sister']
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6]
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6, 867]
['father', 34, 'mother', 32, 'brother', 10, 'sister', 6]
Operators
1. Arithmetic operators
This includes; addition, subtraction, multiplication, division, exponentiation, modulus, floor division
Exponentiation is the raising of a number to the power of another number. The operation is performed by using two asterisks **
.
Modulus is used to get the remainder of a division and is performed using the percent symbol %
Floor division is used to determine quotient of a division. The operation is done using two forward slashes //
a = 4
b = 3
print(a + b) #Addition
print(a - b) #Subtraction
print(a * b) #Multiplication
print(a / b) #Division
print(a ** b) #Exponentiation
print(a % b) #Modulus
print(a // b) #floor division
Output:
7
1
12
1.3333333333333333
64
1
1
2. Comparison operators
These include equal to ==
, greater than >
, greater or equal to >=
, less than <
, less or equal to <=
a = 4
b = 3
# Greater than
a > b
# less than
a < b
# Equal to
a == b
# Greater than or equal to
a >= 2
# Less than or equal to
b <= 1
Output:
True
False
False
True
False
3. Logical operators
These are conjunctions used to combine more than one condition. They include not
, and
, or
a = 3
b = 7
#and
print(a == 4 and b == 7)
#or
print(a == 4 or b == 7)
#not
print(not(a == 4))
Output:
False
True
True
4. Assignment operator
These are used to assign values to variables. They include =
, +=
, -=
, *=
, /=
, //=
, %=
, **=
For the assignment operators excluding =
, the operation on the left is done on the values before they are assigned to the variables.
a = 7
print(a)
a += 2
print(a)
a -=3
print(a)
a *=2
print(a)
a /= 3
print(a)
a //= 3
print(a)
Output:
7
9
6
12
4
1
5. Membership operator
They are used to test whether a value is a member of a sequence i.e. a list, a string or a tuple.
The operators include; in
which checks if a value is a member of a sequence and not in
which checks if a value is not a member of a sequence.
fruits = ['guava', 'banana', 'tomato', 'apple']
print('guava' in fruits)
print('kales' not in fruits)
Output:
True
True
6. Identity operator
They test whether two operands share an identity. They include is
which tests if they share an identity and is not
which tests if they don't share an identity.
print(2 is not '2')
print('4' is not "4"
Output:
False
False
Loops
Loops are used to iterate over a given sequence i.e. lists, tuples or other iterable objects.
if
if
iterates over a sequence when a certain logic condition is met.
count = 0
while count <= 5:
print(count)
count += 1
Output:
0
1
2
3
4
5
for
for
loops can iterate over a sequence of numbers using the range()
function. range()
returns a new list with numbers of that specified range.
for x in range(5):
print(x)
Output:
0
1
2
3
4
while
while loops repeat as long as a certain boolean condition is met.
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
else / elif
else
can be used when a loop fails then the code block else
is executed. elif
, however, is the short of else...if
.
age = 16
if age < 1 and age >= 0:
print("Infant")
else if age >= 13 and age < 21:
print("Teen")
elif age >= 21:
print("Adult")
Output:
Teen
break and continue
break
is used to exit a for
loop or a while
loop.
continue
is used to skip the current block, and return to the for
or while
statement.
count = 0
while True:
print(count)
count += 1
if count = 2:
continue
if count >= 5:
break
Output:
0
1
3
4
Functions
These are groups of related statements that perform a specific task. These help keep things organized and manageable when the program gets larger. They also help avoid repetition and makes the code reusable.
Syntax of a function
def function_name(parameters):
statement(s)
return statement
def
is a keyword that marks the start of a function header.
Next comes the function_name which is used to uniquely identify your function. Naming your function follows the same rules used to name variables.
Parameters are the arguments through which values are passed to aa function.
The colon :
marks the end of a function header.
statement(s) is where your code goes in.
An optional return
statement can be added to make your function return a specific value.
Example of a function and how to call it
def welcome_message(name):
"""
This function welcomes
a user to Python
"""
print ("Hello" + name + ". Welcome to Python!!")
#callng the function
welcome_message(Frank)
Output:
Hello Frank. Welcome to Python!!
Note: Notice how we indent the code that is within our function and not the when we call our function.
Other than creating our own functions which are called user-defined functions, Python has built-in functions that are readily available for use. We have already mentioned a few such as print()
, input()
, type()
, range()
, etc. Below are others, just to mention a few.
abs()
- returns the absolute value of the specified number.
negative_number = -22
print(abs(negative_number))
Output:
22
sorted()
- returns a sorted list of the specified iterable object such as tuples.
tuple = ("h", "b", "a", "c", "f", "d", "e", "g")
print(sorted(tuple))
Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
len()
- returns the number of items (length) in an object.
pets = ['Miles', 'Trevor', 'Kathy']
print(len(pets)
Output:
3
help()
- displays the documentation of modules, functions, classes, keywords. It works for both user-defined and built-in functions. If you are stuck, this function is just a call away!!
There are more concepts in Python such as Stacks, Queues and Object Oriented Programming which require these concepts as a basis. Go practice code and learn some more in whatever sequence!
This content originally appeared on DEV Community and was authored by BochaberiDaisy
BochaberiDaisy | Sciencx (2022-04-23T23:54:58+00:00) Python 101: The Ultimate Python Tutorial For Beginners. Retrieved from https://www.scien.cx/2022/04/23/python-101-the-ultimate-python-tutorial-for-beginners/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.