Python is a simple and easy language to learn. It is one of the languages I recommended to learn in my Learn How To Code post. When learning anything, it is always best to have structure. In my opinion, it would be ideal to learn the fundamental concepts. I will show you the important concepts of programming, but in Python!
COMMENTS
In Python, all comments begin with a ‘#’. The interpreter ignores these. Adding comments to your code may increase its readability.
# print() is a built-in python function.
# You'll learn more about functions later.
print("Hello World!") # Output: Hello World!
VARIABLES
Variables are reserved memory locations to store values. When you create a variable, you are reserving some space in memory.
username = "codeherk"
platform = "Instagram"
Based on the data type of a variable, the interpreter allocates a specific amount of memory and decides what can be stored in it.
message = "Hi" # string
count = 1 # integer
dollars = 10.00 # float
print(message)
print(count)
print(message)
CONDITIONALS
Conditional statements check whether a specified condition is true or false. It determines what event happens next.
# If platform is Instagram,
# print "Follow @codeherk on IG!"
# Otherwise,
# print "Follow @codeherk"
if(platform == "Instagram"):
print("Follow @" + username + " on IG!")
else:
print("Follow @" + username)
LOOPS
Lines of code are executed in a sequential order. Loops allow you to execute lines of code repeatedly until a specified condition is reached.
# Print out each letter
for letter in 'Hello':
print('Current letter: ', letter)
# Current letter: H
# Current letter: e
# Current letter: l
# Current letter: l
# Current letter: o
QUESTION: This code block executed how many times?
FUNCTIONS
A function is a block of organized, reusable code. Python has built-in functions like print(), but you can create your own. When printSum() is called, it executes the instructions in the defined code block.
def printSum(num1, num2):
sum = num1 + num2
# format() is a built-in function
message = "Sum of {} and {} is {}".format(num1, num2, sum)
print(message)
printSum(10,20) # Output: Sum of 10 and 20 is 30
printSum(30,40) # Output: Sum of 30 and 40 is 70
QUESTIONS
Is there such a thing as over-commenting?
What other data types do you know?