Updated September 20, 2023
Basic Concepts of Python Programming
Are you looking to step into the Concept of Python Programming? Or do you want to explore new languages? Concept of Python Programming is often one of the first picks for both because it is easy to pick up and has vast capabilities. Python Programming uses a simple object-oriented programming approach and very efficient high-level data structures. Concept of Python Programming also uses straightforward and concise syntax and dynamic typing. If you want a language for rapid application building and scripting in several areas, you need help finding a better alternative than Python.
One of the critical benefits of Python Programming is its interpretive nature. The Python interpreter and standard library are available from the Python website in binary or source form and can run seamlessly on all major operating systems. Python Programming language is also freely distributable, and the same site even has tips and other third-party tools, programs, modules, and more documentation.
The Python interpreter can be easily extended with new data types or functions in C++, C, or any other language callable from C. The Python Programming language works as an extension for customizable applications. This language is so easy to learn because it uses English keywords rather than punctuation, and it has fewer syntax constructions than other programming languages.
Benefits of Python Programming Language
- Interpreted language: the language is processed by the interpreter at runtime, like PHP or PERL, so you don’t have to compile the program before execution.
- Interactive: you can directly interact with the interpreter at the Python prompt for writing your program.
- Perfect for beginners: for beginner-level programmers, Python is an excellent choice as it supports the development of applications ranging from games to browsers to text processing.
Where Python Programming All Began?
Python is also one of the older web development languages, made by Guido van Rossum at the National Research Institute for Mathematics and Computer Science in the Netherlands in the early 90s. The language borrows heavily from C, C++, Smalltalk, Unix Shell, Modula-3, ABC, Algol-68, and other scripting languages. Rossum continues to direct the language progress, although a core development team at the institute now maintains most of it.
Learning Python Programming Language
As mentioned before, English language keywords make up most of the programming in Python. If you master them, you have mastered Python for the most part. This will take some practice, and you need to know the basic concepts before you start. So let’s begin by looking at them:
1. Properties
Python is implicitly and dynamically typed, so you do not have to declare variables. The types are enforced, and the variables are case-sensitive, so var and VAR are treated as separate variables. If you want to know how any object work, you need to type the following:
help(object)
you can also use the dir(object) command to find out all the methods of a particular option, and you can use object.__doc__ to find out its document string.
Python does not have a mandatory character to terminate statements. Any blocks are specified using indentation, so you indent to start a block and de-dent to the end one. Statements expecting an indentation level end with a colon. If you want to add comments, use the # sign for each line. Multi-line strings need to be used for multi-line comments. Values are assigned using the “=” sign, and equality testing is done with two of them, “==”. You can decrement or increment values with the operators += or -= with the amount on the right-hand side. This can work on strings and other data types. You can also use multiple variables on one line, like so:
2. Data types
Let’s move ahead to data types. The data structures in Python are dictionaries, tuples, and lists. Sets can be found in the sets library that is available in all versions of Python from 2.5 onwards. Lists are similar to one-dimensional arrays, although you can also have lists of other lists. Dictionaries are essentially associative arrays or hash tables. Tuples are one-dimensional arrays. Now, Python arrays can be of any type, and the types are always zero. Negative numbers start from the end to the beginning, and -1 is the last item. Variables can also point to functions. Here is an example of the usage:
You can use the colon to access array ranges. If you leave the start index empty, the interpreter assumes the first item, so the end index assumes the last item. Negative indexes count from the last item, so -1 is seen as the last item. Here is an example:
Adding the third parameter will see the Python step in the N item increments instead of one in the last line. For instance, in the above sample code, the first item is returned and then the third, so items zero and two in zero-indexing.
3. Strings
Let’s move on to strings. Python strings can either use single or double quotation marks, and you can use quotation marks of one kind in a string using another kind, so the following is valid:
“This is a ‘valid’ string.”
Multi-strings are enclosed in single or triple-double quotes. Python can support Unicode right from the start using the following syntax:
u”This is Unicode.”
To fill strings with values, you can use the modulo (%) operator and then a tuple. Each % gets replaced with a tuple item from left to right, and you can use dictionary substitutions as well.
print "Name: %s\
Number: %s\
String: %s" % (myclass.name, 3, 3 * "-")
Name: Poromenos
Number: 3
String: ---
strString = """This is a multiline string."""
>>> print "This %(verb)s a %(noun)s." % {"noun": "test", "verb": "is"}
This is a test.
4. Flow Control Statements
Python’s flow control statements are ‘while’, ‘for’, and ‘if’. For a switch, you need to use ‘if’. For enumerating through list members, use ‘for’. For obtaining a number list, use range (number). Here is the statement syntax:
rangelist = range(10)
print rangelist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in rangelist:
if number in (3, 4, 7, 9):
break
else:
continue
else:
pass
if rangelist[1] == 2:
print "The second item (lists are 0-based) is 2"
elif rangelist[1] == 3:
print "The second item (lists are 0-based) is 3"
else:
print "Dunno"
while rangelist[1] == 1:
pass
5. Functions
The ‘def’ keyword is used to declare functions. Optional arguments can be set in the function declaration after mandatory arguments by assigning them default values. In the case of named arguments, the argument name is assigned a value. Functions can return a tuple, and you can effectively return several values using tuple unpacking. Parameters are passed through reference, but tuples, ints, strings, and other immutable types are unchangeable because only the item’s memory location is passed. Binding another object to the variable removed the older one and replaced immutable types. Here is an example:
funcvar = lambda x: x + 1
print funcvar(1)
2
def passing_example(a_list, an_int=2, a_string="A default string"):
a_list.append("A new item")
an_int = 4
return a_list, an_int, a_string
my_list = [1, 2, 3]
my_int = 10
print passing_example(my_list, my_int)
([1, 2, 3, 'A new item'], 4, "A default string")
my_list
[1, 2, 3, 'A new item']
my_int
10
6. Classes
Python supports a very limited multiple-class inheritance. Private methods and variables can be declared with the addition of two or more underscores and, at most, one trailing one. You can also bind names to class instances, like so.
class MyClass(object):
common = 10
def __init__(self):
self.myvariable = 3
def myfunction(self, arg1, arg2):
return self.myvariable
>>> classinstance = MyClass()
>>> classinstance.myfunction(1, 2)
3
>>> classinstance2 = MyClass()
>>> classinstance.common
10
>>> classinstance2.common
10
>>> MyClass.common = 30
>>> classinstance.common
30
>>> classinstance2.common
30
>>> classinstance.common = 10
>>> classinstance.common
10
>>> classinstance2.common
30
>>> MyClass.common = 50
>>> classinstance.common
10
>>> classinstance2.common
50
def __init__(self, arg1):
self.myvariable = 3
print arg1
>>> classinstance = OtherClass("hello")
hello
>>> classinstance.myfunction(1, 2)
3
>>> classinstance.test = 10
>>> classinstance.test
10
7. Exceptions
In Python, Exceptions are handled via try-except blocks [exceptionname]. Here is an example of syntax:
def some_function():
try:
10 / 0
except ZeroDivisionError:
print "Oops, invalid."
else:
pass
finally:
print "We're done with that."
>>> some_function()
Oops, invalid.
We're done with that.
Importing
In Python, external libraries can be used using the keyword import[library]. For individual functions, you can use from [funcname] or [libname] import. Take a look at the following sample syntax:
import random
from time import clock
randomint = random.randint(1, 100)
>>> print randomint
64
8. File I/O
Python programing language comes with a lot of libraries to begin with. For instance, here is a look at how we convert data structures to strings with the use of the pickle library using file I/O:
import pickle
mylist = ["This", "is", 4, 13327]
# Open the file C:\\binary.dat for writing. The letter r before the
# filename string is used to prevent backslash escaping.
myfile = open(r"C:\\binary.dat", "w")
pickle.dump(mylist, myfile)
myfile.close()
myfile = open(r"C:\\text.txt", "w")
myfile.write("This is a sample string")
myfile.close()
myfile = open(r"C:\\text.txt")
>>> print myfile.read()
'This is a sample string'
myfile.close()
# Open the file for reading.
myfile = open(r"C:\\binary.dat")
loadedlist = pickle.load(myfile)
myfile.close()
>>> print loadedlist
['This', 'is', 4, 13327]
Conditions and Variables
Conditions in Python can be changed. For instance, take a look at this condition:
1 < a < 3
This condition checks that a is greater than one and also less than three. You can also use ‘del’ to delete items or variables in arrays. A great way to manipulate and create lists is through list comprehensions, which have an expression and then a ‘for’ clause, followed by a zero or more ‘for’ or ‘if’ clauses. Here is an example:
>>> lst1 = [1, 2, 3]
>>> lst2 = [3, 4, 5]
>>> print [x * y for x in lst1 for y in lst2]
[3, 4, 5, 6, 8, 10, 9, 12, 15]
>>> print [x for x in lst1 if 4 > x > 1]
[2, 3]
# Check if a condition is true for any items.
# "any" returns true if any item in the list is true.
>>> any([i % 3 for i in [3, 3, 4, 4, 3]])
True
# This is because 4 % 3 = 1, and 1 is true, so any()
# returns True.
# Check for how many items a condition is true.
>>> sum(1 for i in [3, 3, 4, 4, 3] if i == 4)
2
>>> del lst1[0]
>>> print lst1
[2, 3]
>>> del lst1
Global variables are called so because they are declared outside functions and are readable without special declarations. However, if you want to write them, you need to declare them at the start of the function with the ‘global’ keyword. Otherwise, Python will bind the object to a new local variable. Take a look at the sample syntax below:
number = 5
def myfunc():
# This will print 5.
print number
def anotherfunc():
# This raises an exception because the variable has not
# been bound before printing. Python knows that it an
# object will be bound to it later and creates a new, local
# object instead of accessing the global one.
print number
number = 3
def yetanotherfunc():
global number
# This will correctly change the global.
number = 3
Conclusion
There is a lot more to the Concept of Python Programming than what is mentioned above. As always, the key to learning programming, especially Python, is to keep practicing and experimenting. Python has many libraries and vast functionality to discover and tap into. You can also find some other great books and resources to get more in-depth about Python. From classes and error handling to subsets and more, your journey to Python has just started. There will be syntax errors galore, but keep going at it and utilize the excellent Python community and resources available, and you will be fluent in it in no time.
Recommended Articles
Here are some articles that will help you to get more detail about the Concept of Python Programming, so go through the link.