Search

Translate

Basics of python

 Why we use python?

1) Python is simple and easy to learn.
2) It is portable that is same code can be used in various operating systems such as linux, windows, etc.
3) User friendly syntax. that is in c and other programming languages there should be header file, main function, semicolon after each line etc. but in python we can print a statement with a single line.
4) Better readability than other languages, as python uses indentation to highlight the blocks of code.
5) python has many libraries. So that we can those libraries just by importing it instead of coding some functions by ourselves.
6) And the only draw back is, as python uses interpreter which translate the high level programming language to machine language line by line the processing speed is a bit slow compared to other languages.

Nowadays, python is used by many companies like google, youtube, intel, cisco, HP, IBM etc.

Basic data types in python:

1) integer, example: 12
2) float, example: 12.5
3) string, example: "hello"
4) boolean, example: True = 1 or False = 0
5) complex number, example: 2+4j, here we should use only the alphabet j.

Assigning variables:

a = 10 #int b = "hello" #string c = 12.5 #float
As you can see above it is very easy to assign a variable in python. But there are some rules for variable name:
1) First letter of variable name should be capital A to Z or small a to z or an underscore(_).
2) No special characters are allowed other than _ 
3) Variable names are case sensitive. Example: age not equal to Age.
4) Variable names can be constructed with digits and letters.
5) Variable name should not be a python keyword. Example: pass, break, continue.

How to get input from the user:

word = input() #string number = int(input()) #integer weight = float(input()) #float
As shown above you can get the input from the user. As default input data type of python is string, if you want to get an input other than string you have to mention the datatype as int or float etc. To make it more convinient you can also ask the user to enter the data required inside the input function as shown below
word = input("Enter a word: ")


How to print something in python:

age = int(input("Enter the age: ") print("Your age is: ", age)
So if you are getting input from the user you can print like this or simply print(age) which prints only the age. else if you want to simply print something without getting input or assigning you can do as below
print("Hello world")


Operators in python:

Arithmetic operator:

1) +   (plus used for addition. Example: a+b)
2) -   (minus used for subtraction. Example: a-b)
3) *  (star used for multiplication. Example: a*b)
4) /   (slash used for division. Example: a/b)
5) %  (modulus - which divides and gives the remainder as output. Example: 5%2 = 1)
6) //   (double slash which won't consider the decimal part. Example: if 3.2 it takes it as 3)
7) **  (double star which is used as power operator. Example: 2**3 = 8)

Assignment operator:

1) = (equal to)

Relational operator:

1) > (greater than)
2) < (lesser than)
3) >= (greater than and equal to)
4) <= (lesser than and equal to)
5) == (equal to equal to)
6) != (not equal to)

Logical operator:

1) and (returns True when both the conditions are true)
2) or (returns True when any one of the conditions are true)
3) not (returns True when the condition is False and vise versa)


Previous
Next Post »