Ready to Start Your Career?

Python Guide: From Zero to Hero

chiheb chebbi's profile image

By: chiheb chebbi

August 19, 2016

Python Guide: From Zero to Hero - CybraryThis is a detailed tutorial designed for coders who need to learn the Python programming language from scratch. In this course, I'll try to highlight many of Python's capabilities and features. Python is an easy-to-learn programming language. Your First Program>>> print "Hello World"Hello World>>> On UNIX:#!/usr/local/bin/pythonprint "Hello World" ExpressionsExpressions are the same as with other languages, as in the following:1 + 31 + (3*4)1 ** 2’Hello’ + ’World’ VariablesVariables are not tied to a memory location like in C. They are dynamically typed.a = 4 << 3b = a * 4.5 if-else# find maximum (z) of a and bif a < b:z = belse:z = a elif statementPS: There is no “switch” statement.if a == ’+’:op = PLUSelif a == ’-’:op = MINUSelif a == ’*’:op = MULTIPLYelse:op = UNKNOWN The while statementwhile a < b:# Do somethinga = a * 2 The for statementfor i in [3, 4, 10, 25]:print iTuples:Tuples are like lists, but the size is fixed at the time of creation.f = (2,3,4,5)# A tuple of integers Functions# Return a+bdef Sum(a,b):s = a+breturn s# Now use ita = Sum(2,5)# a = 7 Filesf = open("foo","w")# Open a file for writingg = open("bar","r")# Open a file for reading"r" Open for reading"w" Open for writing (truncating to zero length)"a" Open for append"r+" Open for read/write (updates)"w+" Open for read/write (with truncation to zero length) Examples of string processing functionsstring.atof(s)# Convert to floatstring.atoi(s)# Convert to integerstring.atol(s)# Convert to longstring.count(s,pattern)# Count occurrences of pattern in s.

Operating System Services

First, when we talk about operating system, we mean the management of computer hardware and software resources and providing common services for computer programs.Python has the power to easily manipulate the system calls, Operating environment, Processes, Timers, Signal handling, Error reporting etc… Let’s start with the Environment Variables:user = os.environ[’USER’] os.environ[’PATH’] = "/bin:/usr/bin" Working directory
os.chdir(path)# Change current working directory
os.getcwd()# Get it
 Users and groups
os.getegid()# Get effective group id
os.geteuid()# Get effective user id
os.getgid()# Get group id
os.getuid()# Get user id
os.setgid(gid)# Set group id
os.setuid(uid)# Set user id
os.fork()# Create a child process
os.execv(path,args)# Execute a process os.execve(path, args, env)
os.execvp(path, args)# Execute process, use default path os.execvpe(path,args, env)
os.wait([pid)]# Wait for child process
os.waitpid(pid,options)# Wait for change in state of child
os.system(command)# Execute a system command
os._exit(n)# Exit immediately with status n

Regular Expressions in PYTHON

DefinitionRegular expressions are patterns used to match character combinations. Take a look at given below table:
Regular Expressions


foo.*# Matches any string starting with foo
d*# Match any number decimal digits
[a-zA-Z]+# Match a sequence of one or more letters
textMatch literal text
.Match any character except newline
^Match the start of a string
$Match the end of a string
*Match 0 or more repetitions
+Match 1 or more repetitions
?Match 0 or 1 repetitions *? Match 0 or more, few as possible
+?Match 1 or more, few as possible
{m,n}Match m to n repetitions
{m,n}?Match m to n repetitions, few as possible
[...]Match a set of characters
[^...]Match characters not in set
A | BMatch A or B (...) Match regex in parenthesis as a group
numberMatches text matched by previous group
AMatches start of string
bMatches empty string at beginning or end of word
BMatches empty string not at begin or end of word
dMatches any decimal digit
DMatches any non-digit
sMatches any whitespace
SMatches any non-whitespace
wMatches any alphanumeric character
WMatches characters not in
w ZMatch at end of string.
Literal backslash
Schedule Demo