Python For Beginners (Part 1)

Introduction : So, Finally i got the time to make a python programming series for beginners where i will try my best to clear basic concepts of the language.I am assuming that you already know arithmetic operations ( additions , multiplication , division , subtraction) and a few very basic mathematics concepts like variables . Don’t worry if you don’t know what a variable is , I will start the series by explaining variables.If you do have a little bit experience with programming or you are a professional programmer in some other language trying to learn python , please feel free to skip topics.I will try my best not to give too much extra and pointless details.

What is a Variable ? A variable is simply a location to store a value. Imagine a empty box in a room, it symbolizes a variable in logical terms and whatever you put in that box is the value! A variable is denoted or declared using an English language alphabet along with its datatype (don’t worry i will explain datatypes as well).Mentioning datatype is not compulsory in Python like it is in other languages such a C++ and Java.

x = 1  
y = 2
z = x + y # z will be equal to 3

Datatypes : The name is self explanatory . When you declare a variable a variable you need to specify its datatype as well. For example if i write x=1 , the compiler doesn’t know whether it is a string or a integer or a floating point number. In most languages we have to specify the datatype as well , but in python this is not the case.

## In C++ we will write something like : int x = 1 ; // this means x of type integer is equal to 1
#In python we simply write
x = 1 # and the python interpreter automatically understands the datatype which should be integer in this case

Datatype tells a programming language how much bits it needs to reserve for that declared variable . Which means that number of bits for int x = 1 will be different from string x = “1” or float x =1 but of course you don’t have to worry about this as compiler takes care of these things. There are many different datatypes for our use such as int , float , string , long int.

int : it is simply any integer ( positive or negative ) float : any number which contains and will contain ( after calculation ) a floating point ( e.g 12.3435). string : For now simply understand that string is a sequence of any characters generally enclosed within “” symbols.String operations are different from other datatypes.

Keywords of a Programming Language : Every programming language has its own set of keywords . Python like others , has its own set of keywords such as print , if , else , for , while and so on….Don’ t worry , you don’t have to memorize all these keywords.All you have to do is understand their purpose and that is it! I will explain a few of them and the concepts associated with them , you don’t have to learn all of these, as you further learn programming your mind will subconsciously memorize them.

Commenting : Whenever a programmer feels a need to further explain the logic she/he has implemented he/she can add comments with code.It isn’t always the case that nobody else will read your code , as a software engineer in a company you will find yourself reading other people’s code and vice-versa.So you have to write clean code which others can understand as well . In short , Comments are sticky notes in your code for explanation. In python we use # to comment out anything . When you run the code the compiler ignores whatever comes after # and it is not executed.

# This is a comment in python
# For the new line
x = 1 # a variable of value 1
# whatever is writing After # is a comment

Writing your first Python Program : Before writing your first python program there are a few things you need to know .

  • Python uses indentation instead of braces (brackets) in the code which makes it more clean and beautiful . This is a very important point , if your code is not properly indented you will get errors !
  • You can use Whatever text editor you want to write the code , you can even use notepad ! (although i wouldn’t recommend it)
  • If you are a beginner simply use the default Python editor known as IDLE (Integrated Development and Learning Environment).
  • Python files have a extension of .py
  • Download Python interpreter from their official website https://www.Python.org

Create a text file and name it anything you want. Change the extension from .txt to .py to make it a python file and open it with python IDLE. Now write the traditional hello world statement !

print("hello world")
print("HELLO WORLD")
# you can use print statement to print anything you want

And execute it ! This will print hello world on the console. If you don’t have a run button , simply open terminal or cmd and navigate to that folder. After that give the following command :

python3 filename.py

And click enter. This should run your code. If you get a error saying “python3 is not defined” check if you have installed the python interpreter or not. If you have installed python then make sure that you have setup the python environment variables properly.

There is also another way to practice python which is through its command line interface. Simply open the cmd or terminal and type Python3 or Python and the python shell will open.

I hope you have understood these basics concepts , try to practice them , look for online problems and try to solve them to clear your doubts. Now before we move to the next topics keep one thing in mind that code works in sequential execution form, which means that code is executed line by line.

Conditional statements : The first thing in conditional statements is the ‘if’ statement which introduced the concept of decision making i.e if a certain condition is fulfilled a block of code will be executed as a result. The next statements are else and elif ( else if).

if a condition q is fulfilled :
Block X of the code is executed
elif a condition t is fulfilled :
Block Y will be executed
else :
Block Z will be executed

Always remember that there is no else without an if statement. Let me clarify this with an example

if weather is nice :
I will play football
elif weather is rainy :
I will stay at home
else :
Who am i kidding , i have no friends to play with

Let me give a code example :

print("Enter a number : ")
number = input()
if number=='4' :
print("The number you entered is 4")
else :
print("The number you entered is other than 4")

The output of the above code in my console is :

┌─[asif221b@parrot]─[~]
└──╼ $/usr/bin/python3 /home/asif221b/Documents/practice.py
Enter a number :
4
The number you entered is 4
┌─[asif221b@parrot]─[~]
└──╼ $/usr/bin/python3 /home/asif221b/Documents/practice.py
Enter a number :
5
The number you entered is other than 4

elif are used when we want to give more than one conditions , although it is not necessary for else to get executed.

Originally published at https://a51f221b.hashnode.dev.

--

--

--

I am a Cyber Security Engineer(currently studying) and a technology enthusiast.Looking forward to engage with the Medium community and share knowledge!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My experience as a beginner in Elixir. And why you should try it too…

Consensus in Distributed systems

Connecting interactive actors in different rooms across Network in UE 4.22

Basic Docker commands

Fundamental Principles of Synthetic Monitoring

What Is Custom Software Development?5 Best Practices For Developing Custom Software

Important Built-in-methods to Override for your Python Class:

Way to ActivityStarter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
A51F221B

A51F221B

I am a Cyber Security Engineer(currently studying) and a technology enthusiast.Looking forward to engage with the Medium community and share knowledge!

More from Medium

Tuple in Python : Creating, Accessing Tuple

Python list, tuple, and set.

“They love me…!” said Python, with a smile…

Tuples in Python