# Python Guide: From Zero to Hero

August 19, 2016 | Views: 15136

This 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.

>>> print “Hello World”
Hello World
>>>

On UNIX:

#!/usr/local/bin/python
print “Hello World”

Expressions

Expressions are the same as with other languages, as in the following:

1 + 3
1 + (3*4)
1 ** 2
’Hello’ + ’World’

Variables

Variables are not tied to a memory location like in C. They are dynamically typed.

a = 4 << 3
b = a * 4.5

if-else

# find maximum (z) of a and b
if a < b:
z = b
else:
z = a

elif statement

PS: There is no “switch” statement.

if a == ’+’:
op = PLUS
elif a == ’-’:
op = MINUS
elif a == ’*’:
op = MULTIPLY
else:
op = UNKNOWN

The while statement

while a < b:
# Do something
a = a * 2

The for statement

for i in [3, 4, 10, 25]:
print i
Tuples:

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+b
def Sum(a,b):
s = a+b
return s

# Now use it
a = Sum(2,5) # a = 7

Files

f = open(“foo”,”w”) # Open a file for writing
g = open(“bar”,”r”) # Open a file for reading
“w” Open for writing (truncating to zero length)
“a” Open for append
“w+” Open for read/write (with truncation to zero length)

Examples of string processing functions

string.atof(s) # Convert to float
string.atoi(s) # Convert to integer
string.atol(s) # Convert to long
string.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…

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

Process

 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

Definition

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

 Regular Expressions Description foo.* # Matches any string starting with foo d* # Match any number decimal digits [a-zA-Z]+ # Match a sequence of one or more letters text Match 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 | B Match A or B (…) Match regex in parenthesis as a group number Matches text matched by previous group A Matches start of string b Matches empty string at beginning or end of word B Matches empty string not at begin or end of word d Matches any decimal digit D Matches any non-digit s Matches any whitespace S Matches any non-whitespace w Matches any alphanumeric character W Matches characters not in w Z Match at end of string. \ Literal backslash
Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
1. A good guide to various functions, but not enough to teach yourself Python from scratch.

• I think it’s a pretty decent guide if you’re coming from a similar language. Gives a nice overview of the syntactical differences – enough to start and expand on. You’re right though, definitely not for a full from-scratch scenario.

2. excellent reference 🙂

3. Great info! Can’t wait to really “dig into it” when my shift ends. Thanks much!

4. Excellent abstract. Tks.

5. thanks

Page 3 of 4«1234»

### Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

### Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

### Support Cybrary

Donate Here to Get This Month's Donor Badge

### Cybrary|0P3N

A Different View of Defense in Depth Applied
Views: 3254 / May 14, 2019
The Careful Art of Phishing Emails
Views: 2825 / May 14, 2019
How to Survive the Password Apocalypse
Views: 4701 / May 10, 2019
Hacking Tools You Should Learn in 2019
Views: 8227 / May 8, 2019

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel