Python Guide Part 2

November 28, 2016 | Views: 2478

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

This is a completion of the first article Python Guide Part 1: https://www.cybrary.it/0p3n/python-guide-part/
This article is about very basic and common input/output commands in python programming, as well as statements, itterator, import, and functions.

1/ Let’s start with input:

We use input (input or raw_input) functions to read from the keybord, here are some examples:

first_int = int(raw_input(“give me a number: “))
print first_int # this will print the number you entered
first_str = raw_input(“give me a string: “)
print first_str # this will print the string you just typed
type first_str # type string

2/ Now let’s move to the ouput formatting:

first_str = "this"
second_str = "string"
print first_str + second_str # Output: this string

#check this error
#first_str = “our number is : ”
#second_str = 5
#print first_str + second_str
#this will cause error because we can not concatenate a string object with an integer object

#Print Formatting: print “%format” % corresponding_data
first_str = “our number is : %d”
second_str = 5
print first_str %(second_str) # Output: our number is : 5

first_str = int(raw_input(“number : “))
second_str = int(raw_input(“number : “))
third_str = int(raw_input(“number : “))
print “You entered %dt%dt%d” %(first_str,second_str,third_str) # Output: You entered 1 2 3 (if you have typed 1,2,3)

3/import (importing a python module and use its functionalities)

#import (modulename.elementname)
import socket
print(dir(socket))
a = socket.AF_INET
print(help(socket))

4/Condition/Statement

#if statement (it checks for the condition if it's true it will execute the statement)
First_bool = True
if First_bool:
print "True" # >>True
second_bool = False
if second_bool:
print "True" # >>Nothing to print on the screen
if second_bool:
print "True"
else:
print "False" # >>False

5/ loop statement

#while Statement (the while loop statement repeatedly executes a target statement as long as a given condition is true)
First_bool = True
while (First_bool is True):
print "True" # >> True (infinite loop)
break # >> Stop the infinite loop

6/ loop statement

#for itterator (It has the ability to iterate over the items of any sequence, such as a list or a string)
list_word = ["this","is","a","list"] print list_word # >> ["this","is","a","list"]

for word in list_word:
print word # returns the word from list
print list_word, # returns the list as much as there are words in it
print word, # return this is a list

for i in range(10): #range is non-inclusif / 10 isn’t computed
print(i) # this will print numbers from 0 to 9

list_word = [“m”,”y”,”a”,”d”] for letter in list_word:
if letter == ‘a’ :
print “found a!”

7/functions

#functions (function is a block of organized, reusable code that is used to perform a single, related action. They provide better modularity for the application and a high degree of code reusing)
def first_func():
print "hello world!"
first_func() # the function call and the output will be>> hello world!

#expressions
# = assign value
# == compare egality
# != compare inegality
# <> greater than , smaller than

Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Ready to share your knowledge and expertise?
Comment on This

You must be logged in to post a comment.

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.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel