Python Full Course For Beginner's- Part 2 Basics

Welcome back again. If you are new to our series, you can visit the whole series here.

Part 1 Intro

Basics of Python


Your First Program

Our first program will deal with printing a particular text. Let's take "Welcome To Discussbytes" as example

print "Welcome To Discussbytes"{codeBox}

Output: Welcome To Discussbytes

Note: Save your program in .py extension and then run it. 

Python Data Types 

There are basically three types of datatypes in Python. 

  • int-This datatype is used to store integers in a datatype
  • float-This datatype is used to store floating values(in decimals).
  • string- This datatype is used to store alphabets or set of words.

Note: You don't need to necessary use a datatype before a variable.

Example

a=5+9

print(a){codeBox}


Output: 14 

Variables

We need to store each datatype value in a reserved memory location. This reserved memory location is known as variable.

Example: int a
                String b

Here a and b are variables.

Taking Input

Now that we know that how to take values inside a program, we should call a value outside the program. We use input function to accept a value in a variable. By default, a value is accepted as String in Python. So we need to covert it into float or integer if it's a numerical value.

a=int(input("Enter 1st number\n"))
b=int(input("Enter 2nd number\n"))
print("Sum is ",(a+b)){codeBox}

Output: Enter 1st number:4
             Enter 2nd number:9
             Sum is 13
                

Comment

Comments are statement that convey no meaning to our program. It is intended for human use for better understanding of the program.
There are two types of comments:
1. Single Line Comment-Represented with #
2. Multiline Comment- Represented with """        """

#Single Line Comment
print("Single")
""" Multi Line
       Comment
        IN
        Python""
print("Multiline"){codeBox}

Output: Single
             Multiline

Module

A module is simply a file with Python definitions and statements. Any file containing Python coding can be said a module. It can be used by us in other Python programs(reusability of code). There are inbuilt modules as well as user made modules in Python.

We need to use import keyword to use a module in a file.

#Using import and displaying in-built module

#Sample Program

import math

print("Value of pie is ",math.pie){codeBox}


Output: Value of pie is 3.141592653589793

Escape Sequence

It is used to include special characters in the String. 

Here is an example

print("My name is\t"+"Ankur"){codeBox}

 

Output: My name is        Ankur

I would request you to try other yourself. Below our some of the most commonly used escape sequences.

1. \n-For new line

2. \t- For tab

3.\b- For backspace

4.\"- For Double Quote

5.\'-  For Single Quote

Performing Basic Arithmetic Operations

I would request you to try making this program yourself. I will share answer to this program later. You can comment your code below.

Q1. WAP to perform addition, subtraction , multiplication and division by taking user input.

Strings

Whenever we take a String in a variable, each character has its unique value.

Length: It begins from 1. It is used to return the number of characters in a String.

Index: It begins from 0. 

This diagram will help you better in understanding this concept.

python basic syntax


Usually index is used for calling out a character.

a=MY NAME

print(len(a))

print(a[4]){codeBox}

Output: 7 A

Hope I was able to cover all your basics for the remainder of the course. You should now be able to cover python basics questions. If you have any difficulty, suggestion or query, you can tell me in the comment section below. Your appreciation will motivate me in writing more posts.

Keep visiting Discussbytes for the latest Tech updates.

Post a Comment (0)
Previous Post Next Post
–>