Python 3 Tutorial : Python 3 Basics


Let’s start with the Basics of Python :

[information]Note : This tutorial is written with an assumption that you already have a basic knowledge of any other object oriented programming language.[/information]

Python files or modules have the extension of .py. You can execute them in a command line using ‘python filename.py’ where ‘python’ is the name of the Python executable you have in the Python directory.

Things To Know First :

  • In Python, you need to declare datatypes while creating a variable like we do in other programming languages. We can use any type of data to any variable.
  • Indentation and whitespace matters to the Python Interpreter.  In python, Indentation is used for creating block of code which is equivalent to what we do with {} in other programming languages.
  • Python programs are interpreted and not compiled.
  • You can assign data to variables using the = sign.  eg. a = 5.  and a,b = 2,5 where a is assigned with 2 and b is assigned with 5.

Now, let’s see some basic statements :

1. Print Statements :

In python 3, we use a print statement to print a statement to the console.

Example :

print ("any string here")

Output :

any string here

2. Format Replacements :

You can use the {} to replace them with data from variable during runtime.

Example :

name = "Parithi"

print ("Hello {}!".format(name))

Output:

Hello Parithi!

3. If Conditionals :

If statements are used to make conditions.  The true part is executed if the condition is true, if its false, then the ‘else’ block is executed.

Example :

a,b = 3,2

if a > b:

      print ("a = {} is greater than b = {}".format(a,b))

else:

      print ("b = {} is greater than a = {}".format(b,a))

Output:

a = 3 is greater than b = 2

You can also use the If condition like a ternary operator as in C, in form of :

print ("a is greater than b" if a > b else "b is greater than a")

4. While Loop:

A While Loop is used to execute a block of code until a certain condition becomes false.

Example :

b=5

while b<10:

      print ("b = {}".format(b))

      b = b + 1

print("b reached 10")


Output:

b = 5

b = 6

b = 7

b = 8

b = 9

b reached 10

5. For Loop :

The for loop is one another loop which is entry based loop which executes a block of code while the condition is true.

Example :

numberrange = range(0, 10)

for b in numberrange:

       print("b = {}".format(b))

print("End of Operation")

Output:

b = 0

b = 1

b = 2

b = 3

b = 4

b = 5

b = 6

b = 7

b = 8

b = 9

End of Operation

 

We’ll see more functions and statements in the upcoming tutorials…

ElamParithi Arul is Editor At Large and the Owner of Tech Twinklers. He is the founder of the Parithi Network. Basically being a 23 year old Whizkid, He's A Mac User. An Adventurer. A Tech Enthusiast, An Avid PC and PlayStation Gamer, A Hobbyist Photographer and a Nature Lover. His basic interests are developing Websites, Apps and coming up with various creative ideas for a better vibrant tomorrow. He is a professional blogger for about 4 years and writes blogs regarding technology, gaming, and about his own personal life. You can follow him on Twitter at @Parithi

You may also like

LEAVE A COMMENT

Welcome To TechTwinklers!

Tech Twinklers is a Tech Blog run by Students with an aspiring enthusiasm in Technology and Gaming. This Blog will bring News about the Modern Technology, Educational Advances, Campus Interviews, etc. So make sure you subscribe to our blog because we don't want you to miss a thing that is posted here..

We're on Facebook!