Welcome back again. If you are new to our series, you can visit the whole series here.
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
Taking Input
a=int(input("Enter 1st number\n"))b=int(input("Enter 2nd number\n"))print("Sum is ",(a+b)){codeBox}
Comment
#Single Line Commentprint("Single")""" Multi LineCommentINPython""print("Multiline"){codeBox}
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
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.
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.