what is python?
Python is a general purpose programming language language which was created by Guido van Rossum, and released in 1991. Python was created with the aim of making programmer's life easier thus it's simple syntax and readability compared to other languages.
Python has been adopted by many programmers in different fields and has many uses which include;
- Data science
- Artificial intelligence
- Machine learning
- Web development
- Software development
- Python can also be used to connect to databases
Getting started with python
Working with python is extremely easy even for people who don't have basic programming knowledge.
You require an IDE (Integrated development environment) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools and a debugger. You can get the python ide from python.org where there are clear and simple instructions on how to install and run on your computer. There are different versions depending on your operating system and computer specifications. You may also need a text editor to write your code and there are several available in the internet.
After installation you are now set to write your first python code but before that there are things you need to know about python.
Identifiers & Keywords
Keywords are nothing more than special names that exist in Python. When developing a Python program, we can use these keywords to provide specialized functionality.
The following is a list of all of the keywords in Python:
False None True
and as assert
async await break
class continue break
del elif else
except finally for
from global if
import in is
lambda nonlocal not
or pass raise
return try while
with yield
## Variables and data types
Data Types & Variables
Variables are similar to a memory area where a value can be stored. You may or may not modify this value in the future. They include;
- Numbers -numerical data types(float, integers, complex, boolean)
- String - represents alphabets
str= 'hello world'
print(str)
print (str[0])
print (str[2:6])
print (str[2:])
print (str *2)
- List - List items are ordered, changeable, and allow duplicate values.
list= ['abcd ,1234 ,name:Joe']
tinylist =[1234, 'Joe']
print (list)
print (list[0])
print (list[2:6])
print (list+ tinylist)
- Set - A set is a collection which is unordered, unchangeable*, and unindexed.
myset={3,3,3,4,4,5,5,5,23,34,23,65,56,77}
print(myset)
- Dictionary - A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
{}
dict={}
dict['one']='This is one'
dict[2]='This is two'
tinydict={'name':'John','code':'6000'}
print (dict['one'])
print (dict[2])
print (tinydict)
tinydict
print(tinydict.keys())
print(tinydict.values())
- Tuple - A tuple is a collection which is ordered and unchangeable.
The last four are built-in data types in python. More information and resources on variables are data types can be found here https://www.w3schools.com/python/default.asp
I have also written an article on data structures and algorithms in python. https://dev.to/mainashem/data-structures-and-algoriyhms-with-python-7c0 .There is more detailed explanation.
OPERATORS
Python operators are used to perform operations on two values or variables. The following are the various types of operators available in Python;
- Arithmetic Operators addition(+), subtraction(-), division(/), floor division(//), multiplication(), exponents(*), modulus(%).
# simple calculator to demonstrate arithmetic operators in python
num1 = float(input("Enter num1:"))
num2 = float(input("Enter num2:"))
add = num1 + num2 # Addition
sub = num1 - num2 # Subtraction
mul = num1 * num2 # Multiplication
div = num1 / num2 # division
floor_div = num1 // num2 # Floor Division
power = num1 ** num2 # Power Operation
mod = num1 % num2 # Modulus
print("*****************")
print(f"Addition of {num1} and {num2}: {add}")
print(f"Subtraction of {num1} and {num2}: {sub}")
print(f"Multiplication of {num1} and {num2}: {mul}")
print(f"Division of {num1} and {num2}: {div}")
print(f"Floor Division of {num1} and {num2}: {floor_div}")
print(f"Power operation of {num1} and {num2}: {power}")
print(f"modulus of {num1} and {num2}: {mod}")
output
Enter num1:13
Enter num2:7
*****************
Addition of 13.0 and 7.0: 20.0
Subtraction of 13.0 and 7.0: 6.0
Multiplication of 13.0 and 7.0: 91.0
Division of 13.0 and 7.0: 1.8571428571428572
Floor Division of 13.0 and 7.0: 1.0
Power operation of 13.0 and 7.0: 62748517.0
modulus of 13.0 and 7.0: 6.0
Logical Operators;
and - If both the operands are true then the condition becomes True.
or - Returns True if one of the statements is true. Returns False if both the statements or conditions are false.
not - Used to reverse the logical state of its operand. Returns False if the result is true.Assignment Operators - are used to assign values to variables .Remember that unlike other programming languages, Python does not offer increment (++) or decrement (- -) thus assignment operators are used together with arithmetic operators. Example (add and assign +=
>>> num = 10
>>> num += 50 # Same as num = num + 50 where initial value for num is 10
>>> print(num)
60
)
- Comparison Operators - When comparing two values and determining their relationship, comparison operators, also known as relational operators, are utilized. It is mostly used to verify conditions during the decision-making process.
- Equal to (==)
- Not equal to (!=)
- Less than (<)
- Less than or equal to (<=)
- Greater than (>)
- Greater than or equal to (>=)
# Below follow the comparison operators that can be used in python
# == Equal to
42 == 42 # Output: True
# != Not equal to
'dog' != 'cat' # Output: True
# < Less than
45 < 42 # Output: False
# > Greater Than
45 > 42 # Output: True
# <= Less than or Equal to
40 <= 40 # Output: True
# >= Greater than or Equal to
39 >= 40 # Output: False
Membership Operators - Membership operators are used to test if a sequence is presented in an object. Uses in _and _not in keywords.
Identity Operators - Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Bitwise Operators - Bitwise operators are used to compare (binary) numbers:
**
LOOPS
**
A loop allows us to execute a group of statements several times.
for loops
text = "Hello World"
for i in text:
print(i)
#Output
#H, e, l, l, o, , W, o, r, l, d
for i in range(10):
print(i)
#1, 2, 3, 4, 5, 6, 7, 8, 9, 10
while
while 10 > 8:
print("Hello")
while not False:
print("Hello")
while True:
print("Hello")
nested loops
Loops that are nested together are known as nested loops. If we combine a while loop with a for loop, or vice versa, we get a nested loop
**
CONDITIONAL AND CONTROL STATEMENTS
**
Python's conditional statements support the standard reasoning in the logical assertions that we have.
The conditional statements that we have in Python are as follows:
if \elif \else
IF
1
2
3
x = 10
if x > 5:
print('greater')
The if statement tests the condition, when the condition is true, it executes the statements in the if block.
ELIF
x = 10
if x > 5:
print('greater')
elif x == 5:
print('equal')
#else statement
x =10
if x > 5:
print('greater')
elif x == 5:
print('equal')
else:
print('smaller')
When both if and elif statements are false, the execution will move to else statement.
_CONTROL STATEMENTS _
Control statements are used to manage the program's execution flow.
The control statements that we have in Python are as follows:
break
continue
pass
Here are is a link to some examples of control statements in python: https://www.softwaretestinghelp.com/python/python-control-statements/#:~:text=Control%20statements%20in%20python%20are%20used%20to%20control,the%20loop%20and%20proceed%20with%20the%20next%20iterations.
**
FUNCTIONS
**
In Python, a function is a block of code that runs anytime it is invoked. We can also pass parameters to the functions.
A function can return data as a result.
Let's look at an example to better comprehend the concept of functions.
#Function definition is Here
def changeme (mylist):
"This change the value in a function"
mylist.append([1,2,3,4])
print("value inside function,",jkjgfjgm,cf mylist)
return
#now you call the function
mylist=[10,20,30,40]
changeme(mylist);
print("value outside a function")
There are more detailed notes on functions in differentt python documentations on the internet.
Here is a to a sample : https://www.bhutanpythoncoders.com/functions-in-python-organize-code-into-blocks/
FRAMEWORKS
Python frameworks are a collection of modules or packages that assist in the development of web applications and enable the automation of the common implementation of certain required solutions, allowing users to focus more on the logic of the application rather than the basic processes involved in a routine, ultimately making things easier for web development enthusiasts by providing a proper structure for app development.
A web framework is a piece of software that allows you to create web applications. The client-side and server-side programming material is stored in the web framework.
The databases and their associated controls are loaded onto the server. The GUI elements are taken in by the client-side. The term "web framework" refers to a consistent approach for creating websites.
An API functions as a messenger, carrying the user's request to the database, where it is gathered and returned to the user by the receiving system.
Some of the frameworks in python include:
Django
Flask
CherryPy
Pyramid
Web2Py
Bottle
Grok
TurboGears
Tornado
Hug
Dash
Python can be regarded as the future of programming languages as it is evolving everyday and it's easy usability will ensure more people use it to make the world a better place.
Top comments (0)