What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
- web development (server-side),
- software development,
- mathematics,
- system scripting.
What can Python do?
- Python can be used on a server to create web applications.
- Python can be used alongside software to create workflows.
- Python can connect to DBMS. It can also read and modify files.
- Python can be used to handle big data and perform complex mathematics.
- Python can be used for rapid prototyping, or for production-ready software development.
Why Python?
- Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
- Python has a simple syntax similar to the English language.
- Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
- Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
- Python can be treated in a procedural way, an object-oriented way or a functional way.
Good to know
- The most recent major version of Python is Python 3. However, Python 2, although not being updated with anything other than security updates, is still quite popular.
Python Syntax compared to other programming languages
- Python was designed for readability, and has some similarities to the English language with influence from mathematics.
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
PIP:
Pip is python’s package manager. It pulls the modules either from built in modules in python or from the internet(external).
Built-in modules: email, builtins
External modules: tensorflow
First Python program:
Note: The filename that we choose should not be the same as module names like flask, pandas etc.
Printing “hello world” command:
print( “hello world”) ———> Run
o/p: hello world
Comments:
Comments are those lines of text or code that the interpreter ignores.
We use ‘#’ to change any line of text or code into comments. Usually used for single line comment.
However, for multi-line comments, we make use of,
“””
“””
The line of code enclosed between these quotes will be ignored by the interpreter.
Now, let’s suppose, we want print two lines of code as follows:
print( “ subscribe”)
print( “ siddhesh”)
—> Run
o/p:
subscribe
siddhesh
As, we can see, the two print statements will be printed on 2 separate lines.
Now, if we want to print both the print statements in as one, follow the line of code below,
print( “subscribe”, end=” “)
print(” siddhesh”) —→ Run
o/p:
subscribesiddhesh.
If we want to have space between the two lines, we can add space within the enclosed quotation marks here: end=” “. Same can be done if we want to insert a comma, full stop or any other character. We just need to enclose these characters within “”.
Now, if we want to include both the lines of code as one print statement, we can do following:
print( “subscribe” , “siddhesh”)
o/p: subsribe siddhesh
Comma by default creates a space between the two print statements.
Escape sequence characters: These are the characters when used within strings behave in a special way.
Eg: \n is new line character.
use: print (”subscribe \n sid”)
o/p: subscribe
sid
Variables, datatypes and typecasting:
Variable is a container. There are a number of variable types such as integer, string, floating point and boolean.
string: var1= “hello world”
integer: var2 = 4
floating: var3 = 4.1
Type function:
It shows the type of variable.
Eg: print(type(var1)) ——> op: because var1 is a string variable.
Suppose we have 2 variables,
var1= “hello world”
var2 = “15”
print(var1+var2) —→ hello world 15——> Interpreter sees the two variables as string and has no trouble in concatenating them.
Typecasting:
Suppose, we have two variables, var1 and var2 as string variables.
var1 = “ 53”
var2 = “ 12”
When we add both of these, we would ideally want the sum but we get “5312” as output. Why? Because, both the variables concatenate instead of adding up as they are string variables.
To solve this, we make use of type casting. We can typecast ‘str’ into ‘int’ variable and make the addition possible in our case.
print( int(var1) + int(var2)) ——> 65
Suppose, we create a program where we ask for an input from the user and print it on the screen.
print( “enter your number”)
inpnum = input() ——> function to take input from the user.
print( “You entered” , inpnum)
The above program will run without any error. But, if we had to add 10 to the input and then output the result, it would not run.
Why?
Because, input() function by default takes string values and string values cannot be added to integer values. So, we typecast the inpnum variable to ‘int’.
print( “You entered” ,int( inpnum)+ 10)
Calculator Program: