Skip to main content

Posts

Showing posts from January, 2020

Type Casting and Type Conversion in python

Difference between Type Casting and Type Conversion The basic difference between type casting and type conversion is that type conversion is made automatically made by the interpreter or compiler whereas type casting is to be explicitly done by the developer. We need type casting and type conversion when we need to convert the type of one variable to another type. When we want to perform addition or subtraction,the datatype of both variables . Type casting  can be defined as, casting of one data type to another data type, by the programmer, at the time of program design. Automatic Type Conversion(Implicit Type Conversion) The interpreter only converts lower datatypes into higher datatypes.It cannot automatically convert higher datatype to lower datatype . It is also called Implicit Type Conversion We can understand it by taking a real world example.Suppose that there is a person in a company who is working at a higher post, he cannot be automatically demoted b...

Taking user input in python

Taking  user input in python Programmers and developers might often need a data to be entered by the user. Like you  might have seen many websites asking for your username and password for logging in such Facebook, Twitter, Gmail, etc. You need to enter your details to use these websites.This is just one application of importance of user inputs. This will stop you at a certain point and will move forward when you enter details To take user input from keyboard we just need to use an inbuilt function input() in our program Note: Here we are discussing python 3.0 and above .The older versions have some different format so don't confuse with them  SYNTAX: input ( prompt ) eg.  print ( "Enter your username" ) username= input () print ( "Enter your password" ) password= input () print ( "Your username is :" , username) print ( "Your password is :" , password) Output: Enter your username mohit@yopmail.com Enter your password 12...

Variables in Python and checking the type of variable

What are Variables? Variables are nothing but the reserved memory locations where you can store some values. When a variable is created it automatically allocates some memory for itself. How declaration in python is different from other programming languages?? If you have been to other programming languages previously like java ,c or c++ then you need to give the datatype of the variable you want to use.  But Python is a bit more flexible and intelligent you need not to specify the datatype of variable, python will automatically detect the datatype of variable will perform the operations accordingly. Python variables do not need explicit declaration to reserve memory space. Assigning values to variables  You can simply assign values to variables by just putting an equals to(=) sign between variable and value.You can simply create a python file in pycharm  eg. a= 10 # An integer variable b= 45.67 # floating point variable c= "Hii this is a strin...

Escape Sequences in Python

What are Escape Sequences? Escape  Sequences refers to the combination of characters that begin with the backslash( \). They are non-printable characters which convey some special meaning to interpreter like to change a line , leave seven spaces etc.An escape sequence is used when writing sections of code, like preprocessors definitions, to specify continuation characters,so that multiple lines of codes are consider as a  single line by interpreter. Escape  Sequences are basically used for designing purposes. They can be used between the statements. An escape sequence contains  more than one character but functions as a single character because there is no letter to textually represent escaped character. How to use Escape Sequences? The following is the list of some Escape sequences with the examples: \n          It is used to change the line. You can use it anywhere in the middle of statement. eg. >>> ...

Using Python as calculator and some operators

Using Python as calculator Python has the very large number of libraries and each library has  its own  specific use. But before heading towards advance Python, we must be knowing the basics of it. What you have to do is to simply open your Python Console in pycharm and we are all set to go . In the Python Console as well as in our examples , input and output are distinguished by the presence or absence of prompts ( >>>  and  … ): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with the prompt are output from terminal .And we will be giving comments with #.  Click here  to know more about comments. Now, open your python console and wait for the prompt( >>>   ) to appear.(It must not take too long to appear) eg. #Some basic operations >>>   67+89  ↵  # Input 156                ...

Giving comments in python

What are Comments ? Comments are some statements or some expressions that we write for enhancing the readability of our code and making our program easier to understand.  Python interpreter just simply ignore these comments and they doesn't become the part of program.  We can simply say that we write them for simplicity and clarity of code.So that we face no problem in understanding them in future and comments also help other people to easily understand your code. How to give comments in python? There are basically two types of comments :- 1) Single Line Comment       These are the comments which we give in a single line.     These can   be given by simply given by writing :      #  followed by the statement that you want to give eg.  # I am a comment and python interpreter will definitely   ignore me  eg. print ("This is the comments tutorial")       # I am a print stat...

Attributes of print statement in python

Python print() statement In our previous tutorial we have seen how to use print() statement. Click here!  if you haven't seen that tutorial. The full syntax of print() statement is   print (" *  The statement to be printed", sep ="The separator you want to insert between words", end ="What you want to print at end of line", file=sys.stdout , flush =False) Here,   * - Here * means that we can give  more than one print statement separated by commas sep -  Separators can be used when uou want to sepereate  Words by some specific character. Default value of separator is " " (single space)     end- It simply indicates that how do you want to end your line. Default value of end="\n"(leave a single line)   file-- We use this attribute to write something into a file.  That             text will not be displayed in the terminal.       ...

Hello World : Creating our first python program

HELLO WORLD! Create first python program  In our previous tutorials we have seen the installation and setup of of Pycharm and IDLE Saying Hello World! to Python Now we will run our first program and see how to print hello world in python. Step 1) Open your PyCharm Editor  and  click on "Create New Project" Step 2)     You will need to select a location. You can select the location where you want the project to be created. If you don’t want to change location than keep it as it is but at least change the name from “untitled” to some other name . PyCharm should have found the Python interpreter you installed earlier.  Next Click the “Create” Button Step 3) Now go to "File"  menu. Select "New".Then select "Python File"   Step 4) A pop-up will appear ,write the name of your Python File and hit enter. Step 5)   Now type a simple program                ...