Home 0P3N Blog Python Guide Part 2
Ready to Start Your Career?
Create Free Account
By: ziednamouchi
November 28, 2016

Python Guide Part 2

By: ziednamouchi
November 28, 2016
By: ziednamouchi
November 28, 2016
fractal-1303448_1280This 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 enteredfirst_str = raw_input("give me a string: ")print first_str# this will print the string you just typedtype first_str# type string2/ 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_datafirst_str = "our number is : %d"second_str = 5print first_str %(second_str)# Output: our number is : 5first_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 socketprint(dir(socket))a = socket.AF_INETprint(help(socket))4/Condition/Statement#if statement (it checks for the condition if it's true it will execute the statement)First_bool = Trueif First_bool:print "True"# >>Truesecond_bool = Falseif second_bool:print "True"# >>Nothing to print on the screenif second_bool:print "True"else:print "False"# >>False5/ loop statement#while Statement (the while loop statement repeatedly executes a target statement as long as a given condition is true)First_bool = Truewhile (First_bool is True):print "True"# >> True (infinite loop)break# >> Stop the infinite loop6/ 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 listprint list_word,# returns the list as much as there are words in itprint word,# return this is a listfor i in range(10):#range is non-inclusif / 10 isn't computedprint(i)# this will print numbers from 0 to 9list_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
Request Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry