What comes to your mind when a word like python is mentioned ? Snakes right...
For now let's talk about programming leave alone about the snakes.
Did you know that Python language is the most growing programming language in the world of programming.Be it web development, gaming, software development, machine learning, data analysis, data science ,Artificial Intelligence and more trending and upcoming crazy stuff in the globe?
Let's dive in.
What is Python?
Python is Python is a high-level, general-purpose programming language that is interpreted.
It is considered as "battery included" language because of its comprehensive standard library.This helps in supporting many programming paradigms like structural,object-oriented and functional programming.
Guido van Rossum started working on Python in the late 1980s as a replacement for the ABC programming language, and Python 0.9.0 was released in 1991.
Advantages of Python.
- Easy to read, learn and write.
- Highly productive due to its simplicity to help developers focus on solving world problems.
- An interpreted language which means that Python directly executes the code line by line.
- Has a vast libraries support of over 200,000 packages.
Disadvantages of Python
- Slow in speed since python execute line by line of code.
- Python programming language uses a large amount of memory.
- Weak in mobile computing.
Before we dive deep into the essentials, kindly make sure that you have installed and set up your Python, visual code editor and jupyter notebook or anaconda are standby and ready.
Python Fundamentals.
- Data types
- Compound data structures
- Conditional,loops and functions
- Object-oriented programming and external libraries
Data types.
-
int
(Integer)
Just numbers without a decimal.
-
str
(strings)
-
float
(floating point numbers)
-
bool
(booleans)
Compound data structures
- List
This is a collection which is ordered, they are changeable, can be duplicated.
- Tuple
A collection of ordered items but they are unchangeable.
- Dictionary
A collection that is unordered, changeable and indexed.
- Set
This a collection of unordered, unindexed items but they are changeable and doesn't take duplicate values.
Conditional,loops and functions
- The if statement
if BOOLEAN EXPRESSION:
STATEMENTS
an example
amount = 400
if amount == 400:
print("The tax price is " (amount*(16/100))
- The if else statement
It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false. For that we have the if else statement.
food = 'spam'
if food == 'spam':
print("Ummmm, my favourite!")
else:
print("No, I won't have it. I want spam!")
- Chained conditionals
This is when there are more than two possibilities, if choice one is not true or false select choice two if choice two is not select three.
var1 = 100
if var1:
print ("1 - Got a true expression value")
print (var1)
else:
print( "1 - Got a false expression value")
print (var1)
var2 = 0
if var2:
print ("2 - Got a true expression value")
print( var2)
else:
print ("2 - Got a false expression value")
print (var2)
print ("Good bye!")
- Nested conditionals
A nested if statement is an if statement that is nested (meaning, inside) another if statement or if/else statement. Those statements test true/false conditions and then take an appropriate action).
if x < y:
STATEMENTS_A
else:
if x > y:
STATEMENTS_B
else:
STATEMENTS_C
Practical example
if 0 < x: # assume x is an int here
if x < 10:
print("x is a positive single digit.")
- The for loop
The for loop processes each item in a sequence, so it is used with Python’s sequence data types - strings, lists, and tuples.
Each item in turn is (re-)assigned to the loop variable, and the body of the loop is executed.
for LOOP_VARIABLE in SEQUENCE:
STATEMENTS
practical example
for x in range(0, 3):
print("We're on time %d" % (x))
x = 1
while True:
print("To infinity and beyond! We're getting close, on %d now!" % (x))
x += 1
- Functions
A function is a block of code that performs some operations on input data and gives you the desired output.
Functions are used in working on big projects to make maintainance of the code to be easier and real task. If a code performs similar tasks many times, a convenient way to manage your code is by using functions.
Object-oriented programming and external libraries
Objects are instance of a class and are defined as an encapsulation of variables (data) and functions into a single entity. They have access to the variables (attributes) and methods (functions) from classes.
Here is how you define a class and an object of it:
class Rectangle:
def __init__(self, height, width):
self.height = height
self.width = width
def area(self):
area = self.height * self.width
return area
rect1 = Rectangle(12, 10)
The attributes and methods can be accessed using dot(.) operator.
rectl.height
rectl.width
rectl.area()
External libraries/modules
Python has vast modules that are defined classes,attributes and methods that we can use to accomplish multiple tasks. For example, the math
library has many mathematical functions that can be used to perform various calculations. matplotlib
used for plotting.
import math as m
print(type(m))
output is
<class 'module'>
using matplotlib
That's all for today Happy coding!!
Top comments (0)