Get started with Python For Beginners: Part 1
Learn the basics of programming in Python in no time.
If you are just a beginner or if you already are an experienced programmer, you will find Python to be the easiest language ever. Just like Pseudo code. So let’s let’s jump right in!
Note: This post and all others in the future will be for Python 3.
What’s is the first thing that comes to your mind when you think of writing any code? You guessed that right, it’s variables!
Other guys: But we need Data Type to declare variables!
Python guy: Hold my duck!
Variables
In Python, any variable can take any type. You don’t have to explicitly declare variables with data types in order to use them. This is why we say, Python is dynamically typed. If used in the the context of object oriented programming, we call this duck typing. But let’s keep it simple for now.
x = 'Hello world' #var x is string
x = 5 #now it has become integer
Conditions & Loops
For both conditions and loops, the very first thing that needs to be taken care of is the Indentation. If overlooked, it can lead to some very unusual and embarrassing problems in the code.
Here’s how you will write conditions:
# If-Elif-Else Conditions
if x==5:
print('Variable x is 5')
elif x>5 and x<8 :
print('Variable x is between 5 and 8')
else:
print('Variable x is something else entirely!')
Another thing to note here is that we use 'and', 'or', 'not' instead of '&&', '||', '!' logical operators in Python.
Now onto Loops...
There are two types of loops in Python: While and For.
# While loop
i=1
#Prints all numbers from 1 to 10
while i <= 10:
print(i)
i=i+1 # remember here, i++ / i-- etc. don't work in python
Note: i++, ++i, i--, --i have no meaning in Python. Instead, we use i=i+1, i+=1 , i=i-1, i-=1
# For loop
i=1 #initialize i with 1
#Prints all numbers from 1 to 10
for i<=10:
print(i)
Pretty easy, right? That's it for now. In the next part, we'll look at collections, that is, the heart of coding!