Introductory code in Python

Depending on your Operating System, you can download Python, by following this link:

Python Downloads

Then, you can start your journey to this enriched and flexible language, by playing around with the code below.

Copy and paste the following code to your Python shell or IDLE:

# It became almost a tradition to start a new language, by learning how to
# code a ‘Hello World’ statement in it. So, let us start:

print(“Hello World!”)
print (1)

# Variables, name = value

name = “James”

name = “Let’s change your name to Harvey!”

hello = “He said: ‘Hi…’\nShe said: ‘What?'”
# Creating a new line in the output

hello = “He said: ‘Hi…’\tShe said: ‘What?'”
# Creating a tab space in the output

firstNumber = 1

# Floating point numbers
firstFloat = 100.0

secondNumber = 5

addingNumbers = firstNumber + secondNumber
dividingNumbers = firstNumber / secondNumber
print(addingNumbers, dividingNumbers)

# % finding the remainder of the division= modulus calcualtion

modNumbers = firstNumber % secondNumber

# Functions in Python

# def for defining a function
#def functionName (optionalInput, optionalInput2, …, ):
# Optional Code
# Optional Return
# more Code

# the tab space is really important in Python, since it indicates that we
# are still within the function

def addOne(number):
temp = number + 1
return temp

example = addOne(10)
anotherEx = addOne(13)
lastEx = addOne(firstNumber)
print(example, anotherEx, lastEx, sep = ‘/’)

# Loops in Python:
# ‘For in’ and ‘While’ loop

# for nameOfTempVar in loopOverObject:
# someCode
# more Code

for number in range(0, 11, 1): #start, stop, step
print(“Line”) # just to grasp exactly how the loop

# We can also neglect the start and step value (they are optional)

for number in range(4): #start, stop, step
print(“New Line”)

# while Variable condition Variable/constant:
# some Code
# more Code

counter = 0
while counter <=10:
print(“The current value of the counter is: “, counter)
counter +=1 #identical to the argument: counter = counter + 1


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s