Basics of Python

In this part we are going to understand the basics of Python. These basics can really help you while starting coding in Python. Python is a lot different from the other programming languages in terms of writing style and syntax. 


How to print ?

To print something in Python you need to use the keyword "print( )"

  • To print a simple string -

print("Hello Coders!")
print('Hello Coders!')

Hello Coders!
Hello Coders!


You can either use double quote [ " ] or single quote [ ' ] both will work same in Python.

  • To print a variable - 

a = 20
print(a)

20


  • To print a string with a variable -

a = 20
print('The value of a is ',a)

The value of a is 20


After understand how to print in Python, now lets understand how to initialize variables.


Variables

A variable is a name given to the memory address of your system whose value is not fixed and can be altered at the run-time. 
To declare a variable in Python first you need to know the rules to name the variable.

Rules to name a variable -

  • It must start with a letter or underscore( _ ).
  • It shouldn't start with a digit.
  • A variable name can only contains alpha-numeric characters and numbers (A-z, 0-9 and _ ).
  • Variable names are case sensitive.
Some examples of Valid Variables - a, b, var, _variable, x2, the_variable.
Some examples of Invalid Variables - 1a, n@me, x-a, the variable.


Initializing a Variable

To initialize a variable we need to use '=' operator.

x = 20
a = 'Hello Coders!'

Also to initialize multiple variable at the same time we can do  

x,y,z = 20,10,30
a,b,c = 'Hello','Coders','!'
where x, y, z is equal to 20, 10, 30 and a, b, c is equal to 'Hello', 'Coders', ' ! ' respectively.

To take User Input

To take values from the user, we have to use input( ).
x = input()
To take only the integer type input, we can do
x = int(input())
To show some string on the time of taking the input, we have to write that string inside of input( ) within Single or double quotes.
x = input('Enter a string: ')
y = int(input("Enter an integer: ")

Now let's study about the concept of "Global Variable".

Global Variable

A 'Global Variable' is a variable which can be used in or outside of the function. Basically its scope is larger than a Local variable. Any variable in Python which is declared outside of a function is a global variable. We can also convert a local variable to a global variable by using a keyword 'global'.

[NOTE: 'Local Variable' is preferred over 'Global Variable' ]

Let's understand this with some examples -
x = 20
def function( ):
    x=10
    print(x)

print(x)

10
20

(Here in the first print( ) statement the local value of x have the higher priority over the global value of x)
def function( ):
    global x=10
    print(x)

print(x)

10
10


(Here we are increasing the scope of x into the global scope, so it's value can exist outside the function)
x = 20
def function( ):
    global x=10

print(x)

10

(Here first the value[Global Value] of x=20 but after the function call the value of x is changed to 10)

From these examples you have got the idea of how to and why we use "Global Variable".

Indentation

Indentation is very important and have a crucial role in Python. Instead of using the parenthesis {} in Python, we use "Indentation". It's work is similar to the work of parenthesis i.e, to describe scope of the statements under which they lie. Basically an 'Indentation' is a [tab] space. Let's understand it by an example.

def function( ):
    global x=10
    print(x)

print(x)
Here we can see that the Second[global x=10] and the third line[print(x)] are having some space before them, it signifies that the both statements are inside the function. But the last line has no space in front of it signifies that this statement is outside of the function.

Let's take an another example-
if(10>5):
    print('10')
else:
    print('5')
Here also the line 2 and the line 4 have a [tab] space before them signifies they are inside the 'if statement' and 'else statement' respectively.

#Comments
Comments are those piece of codes that are ignored(not executed) by the compiler, they are just used to write some notes and important part during programming which can help the other programmers to understand the code easily and in a better way.
In Python you can comment by putting a ' # ' before that string. Let's take an example.
#This is a Comment

x=20
print(x)

If you want to make a multiple line comment then you can do as follow.
"""
Hello Coders!
This is a Multi-line Comment.
"""
print("Comment")
If an string is not assigned to a variable, Python will ignore it and we can use this to make Multi-line Comment.

Those are some basic things you need to know before staring coding in Python.



For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time... 

Post a Comment

1 Comments