Basic Concepts of Pythons
What is Python?
Python is high-level programming language. It has been used in numerous areas, including web programming,
scripting, scientific computing, and artifical intelligence.
There are three major versions of Python. They are Python1.x, Python2.x and Python3.x
Let's start learning Python. You need to write yourself and run the code. NEVER EVER copy and paste the code.
Your First Program
Just like other programming languages, Let's start with "Hello World !" program.
What is "Hello World Program"?
It is the first program in nearly every programming language for every programmer.
It displays the words, "Hello World". It doesn't really print anything on the paper. In Python, it looks like this:
print("Hello World")
After writing the code above in your program, click on run button.(click enter if you are using command promp).
The result (which is called "output" in programming) is the words
Hello World
Note that the single quotation marks don't appear in the output. The computer displays only the words between them.
If you want to display quotes to the user, then you need \ infront of it.
print("Ko Ko said, \"I'm hungry.\" ")
The print statement gives the user multiple lines of output.
print("Hello World")
print("Hi")
Hello World
Hi
We can use a pair of single quates(' ') instead of double> quotes (" ").
print("Hello")
print('Hello')
print("Ko Ko said, \" I'm hungry \"")
print('Ko Ko said, "I\'m hungry"')
Note that in line 4, we don't need \ infront of " while using ('...') but we need infront of '
In Python, we use backslash to print " inside "..."(' inside '...'). We can aslo use \n for line breaking.
print("This the first line. \n This is the second line. \n Thank you for visiting our page. \n \\is backslah.")
To print backslash, we need to use \\
This is the first line.
This is the second line.
Thank you for visiting our page.
\ is backslah
Data Types and Values in Python
After learning Hello World Program, I believe that you understand the print statement. We need parenthesis after print.
We have to use a pair of either single quotes or double quotes between the parnthesis.
We have to put the text we want to display between quotes. Without quotes, it may lead the error.
Write the following codes and run the program.
print(Love)
You get error messages beacause of lacking quotes.
But you won't see any error message while you run the following code.
print(1500)
This is because 1500 and Love belong to different data types: 1500 is an integer while Love is not. So we need quotes for Love.
print("Love")
In computer programming, a sequence of characters is called a string. In Python, string is always put inside a pair of quotes
(single quotes or double quotes). Love in the above code is string.
A VALUE is one of the basic things a program works with. It may be a letter or a number. Some of the values we have seen are 'Hello World!', 1500 and "Hello".
Different values belong to different types: 1500 is an integer and Love is a string.
1500, "1500", "1500.0" and 1500.0 are all values. The types of these values can be found by using type() function. The type() function doesn't display the result. So we need the print() statement.
print(type(1500))
print(type("1500"))
print(type("1500.0"))
print(type(1500.0))
The output is as follows
< class 'int' >
< class 'str' >
< class 'str' >
< class 'float' >
int means integer (e.g. 1500, 3, 7) str means string (e.g. "Hello World") float means floating points (e.g. 3.14). They are the most common data types for beginner.
We can convert the type of certain values. int() is used to convert into integer. float() is used to convert into floating point. str() is used to convert into string.
print(int("3")+int("2"))
print(float(int(3.14)*2))
print(str(99.99)+str(int("1")+int(77.77)))
The value behind decimal is ignored while converting from floating point into integer.
5
6.0
99.9978
Variable in Python
In "Data Types and Values", we have seen that 1500 is a value and its type is integer. We can asign any value to a variable.
Love = 1500
print(Love)
The first line in the above program is called assignment statement. It assigns the value 1500 to a variable called Love. Python doesn't need quotes to assign integer but it needs quotes to assign string.
A = "Hello everyone"
print(A)
print("A")
In the above program, A and "A" are two different things. A is variable name and
"A" is the string.So the output is not the same.
Hello everyone
A
Python is a case sensitive language. This means that A and a are two different variables. We can use characters, numbers and
underscore to name a variable. We can't use space and symbols (e.g. *, &,...) We can't use numbers at the start of a veriable name.
It is important to remember that we need to assign a value to the veriable before using it.
Operations
Write the following code in your program and run it.
print(5+10)
print('5'+'10')
The output is as follows.
15
510
This is because 5 is integer while '5' is string. Python adds two integers and concatenate two strings. 5 and 10 are called operands and + is called operator. Since + needs two operands (in this example, 5 and 10), it is called binary operator. It can also be called arithmetic operator because it performs mathematical operation on numeric values. +, - , *, / are common arithmetic operators. Note that the addition of two integers give integer but the division of two integers gives floating point.
Python divide operators in the following groups.
- Arithmetic operators(+, -, *, /, %, **, //)
- Assignment operators(=, +=, -=, *=, /=, %=, **=, //=, etc.)
- Comparison operators(==, !=, >, <, >=, <=)
- Logical operators(and, or, not)
- Identity operators (is, is not)
- Membership operators(in, not in)
- Bitwise operators (&, |, ^, ~, <<, >>)
As a beginner, you have to understand Arithmetic operators, Assignment Operators and Comparison operators.
+ concatenate two strings. Remember that print("5"+'10') gives the output 510. In mathematics, 5*3 =5+5+5= 15. This is the same thing in string. "5"*3 will give you "555" (because "5"*3="5"+"5"+"5"="555")
+ and - serves as unary operator, which takes only one operand.
print(+5 * -3)
Remember that we need to use print() function to display the output. In this example, + takes only one operand 5 which means positive 5. - takes only onle operand3 which means negative three.
Nice and perfect explain......
ReplyDeleteThanks for everything sir.....
I have learned much.......Really worth it......(^ ^)
Thank you so much for your support. We are trying our best to help you with learning.
Delete