Mastering any programming languages requires one to learn a ton of skills. It is not only about coding in the language but also learning various concepts which may come handy in different problem solving scenarios. You can learn more about python using it's Python Documentation and Tutorial. Throughout this write up we'll be looking at the various concepts one should learn while mastering Python the right way.
These Python concepts will go from beginner to advanced concepts. To master them needs practice.
Variables
Variables are containers for storing data or values:
a = 5
You can get the datatype of variable using type()
type(a)
<class 'int'>
Naming variables becomes a major concern when working with large programs and you need descriptive names to have readable code.
A variable like a
may be hard to understand it's function later when you are reviewing the code or when code is handed over. A variable like idNumber
may be easy to understand.
Here are a few additional rules to remember when working with Python variable names:
- Should start with a letter
a-z
or the underscore character_
. - Should not begin with a numerical value
0-9
. - Variable names are case-sensitive (book, Book and BOOK are considered as three different variables)
Conditions
Python supports the use of the following logical conditions :
Equals:
a == b.
Checks if two values are equal and output True
or False
.
Not Equal:
a != b
Checks if two values are not equal and print True
, otherwise outputs False
Less than:
a < b
Checks if the first value a is less than b
Less than or equal to:
a <= b
Checks if value a is less or equal to b
Greater than:
a > b
Checks if value a is greater than b. If true it outputs True
Chained Conditionals
Python allows for nested selections of conditions which is referred to as chained conditionals
if a < b:
print("a is less than b")
elif a > b:
print("a is larger than b")
else:
print("a and b are equal")
The conditions are nested using if, elif else if
and else to check if all conditions are met
Operators
Operators are used to perform operations in values and variables. Here is a list of operators:
Arithmetic operators:
-
addition
,-subtraction
, /division
, %modulus
, *multiplication
.
Assignment operators: =, +=, -=, *=, /=.
Comparison operators:
- == Equal a == b
- != Not equal a != b
- > Greater than a > b
- < Less than b < a
- >= Greater than or equal to >= b
- <= Less than or equal to b <= a
Identity operators:
Used to compare objects, not in terms of equality but if they refer to the same object
a is b
, a is not b
Logical operators:
and, or, not. and
between two conditions e.g if a=10 and a>9 means both conditions must be met in order to continue. Or
chooses if either one of the conditions are met to allow continuation of the program.
Control Flow (If/Else)
Control flow determines the order in which a program code
executes. The control flow of a program is regulated by conditional statements, loops, and function calls.
if else statements
if condition/expression:
statement to be executed
elif condition/expression:
statement to be executed
elif condition/expression:
statement to be executed
else:
statement to be executed
if a < 0:
print "a is negative"
elif b % 2:
print "b is positive and odd"
else:
print "b is even and non-negative"
Loops and Iterables
Looping in Python is handled by for, while, if loops.
for loop
for i in something_iterable:
print(i)
while loop
count = 0
while (count < 3):
count = count + 1
print("Count me")
if-else loop
if (condition):
statements
else:
statements
An iterable is anything you can loop over using a for loop in python. Sequences such as lists, tuples, and strings are iterable.
for i in something_iterable:
print(i)
Basic Data Structures
Data structures define how data is stored and operations to be performed on them. To build great programs, you first need to understand what data structures and algorithms may work on them.
Have a look at An introduction to data structures and algorithms in Python
Functions
Functions are a fundamental to programming. Functions allow us to write blocks of called which only executes when the function is called. To write a function in python the def
keyword is used.
def myFunction():
print("Hello there")
myFunction()
It may also take arguments.
def myOtherFunction(str):
print(str)
myOtherFunction("Hello")
Hello
More topics to learn as you master Python
Mutable vs Immutable
Mutable refers data types whose values can be modified e.g list, dictionary, and sets while immutable are data types whose values cannot be changed once they are described e.g int, float, decimal, bool, string, tuple, and range.
File Input and Output
Involves operations of files necessary for data operations. Some operations are:
1. Opening a file.
2. Reading from the file.
3. Writing data to file.
4. Closing a file after operations of read or write.
5. Creating and deleting a file.
Receiving input from user using input() function
name = input("Enter your name: ")
print (f'You entered {name}')
Enter your name:
Opening files;
f = open('workfile.txt', 'w')
The code example opens up a file named workfile.txt
if it is in your working directory and the w
means it will be opened in a mode for writing
. To read the mode r
for read is used.
To go ahead and read content in the workfile.txt;
content = f.read()
print(content)
And make sure to close the file after operations;
f.close()
Object Oriented Programming (OOP)
Start by looking at my introduction to object oriented programming in python
Data Structures and algorithms from introduction to data structures and algorithms with python
Comprehensions
Comprehensions allow us to create new sequence using another sequence;
aList = [i for i in range(5)]
print(aList)
Lambda Functions
Lambda functions in python are normal functions but they do not have a name and have only one line of code.
lambda x: x+x
x is the argument in the above function
(lambda x: x*2)(12)
24
The argument x is set to 12 outside the function and answer output is 24
This tutorial is a sequel to Object Oriented Programming with Python
Top comments (0)