18
Python 101!, Python Basics
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. The most recent major version of Python is Python 3.
- web development (server-side),
- software development,
- mathematics,
- system scripting.
- Python can be used on a server to create web applications.
- Python can be used alongside software to create workflows.
- Python can connect to database systems. It can also read and modify files.
- Python can be used to handle big data and perform complex mathematics.
- Python can be used for rapid prototyping, or for production-ready software development.
- Python works on different platforms that is windows, linux and IOS.
- Python has an easy to read syntax similar to the English language.
- Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
- Python code can be executed as soon as it is written because it runs on an interpreter system.
- Python can be treated in a procedural, an object-oriented or a functional way.
- Python was designed for readability, and has some similarities to the English language with influence from mathematics.
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
- Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose. For Example print("Hello, World!")
Comments
A comment is used to tell the reader of the code what it is all about and they don't run when the program is executed. In python, we have two types of comments that is single and multiline comments.
Single Line comment:
#python is fun
Multiline comment:
'''
Python
is
fun
for
programming
'''
Variables
Variables are used to store information that is referenced to be used later in the execution of the program thus provide a way of labelling data so the programs can be understood more clearly by the reader.
Variable data types do not need to be declared. Their data types are inferred from their assignment statement, which makes Python a dynamically typed language. This means that different data types can be assigned to the same variable throughout the code.
favourite_car = "Aston Martin"
print(favourite_car)
favourite_car = 26
print(favourite_car)
favourite_car = False
print(favourite_car)
Relational, Logical operator
These are used to determine the action to be done on statements that evaluate to either true or false. For comparison we have ==, !=, <, >, <=, >= And Logical we have and, or, not
Conditionals
This a construct which helps execute a section of code when a certain condition has been met. We have: if, else, elif
# comparison operators:
age = 21
if age >= 21:
print("You can drive a trailer")
elif age >= 16:
print("You can drive a car")
else:
print("You can ride a bike")
Data type
These are keywords used to define the kind of data being stored.
We have several kinds of datatypes that is String, Character, boolean and others
These are used to execute a block of code for a given number of times and we have for, while and do while loops.
# for loop
for x in range(2, 6):
print(x)
for x in range(2, 30, 3):
print(x)
# loop through list
for x in ["Python", "Go", "Java"]:
print(x)
# Nested for loops
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
# while loop
level = 0
while(level < 10):
level += 1
A function is an independent section of code that performs a specific task with in a program and may take some inputs and return outputs.
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
To understand the meaning of classes we have to understand the built-in init() function.
All classes have a function called init(), which is always executed when the class is being initiated.
Use the init() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
#####Example 1
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
#####Example 2
#Using object methods
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
I hope this article gets you started in your journey with the python programming language, thank you.
Check out my random-password-generator project on github.
18