Python Tutorial

pythoncode4you

Python Tutorial




What is Python????

Python is developed by Guido van Rossum in 1989.
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

Why we use Python ????

  1. Easy to use
  2. Easy to learn
  3. Open source
  4. Platform independent
  5. High level programming language
  6. OOPs
  7. Scalability
  8. Modularity
  9. Interpreter

Where is Python is used?

Python is a general-purpose, popular programming language.The various areas of Python use are given below.
  • Data Science
  • Mobile Application
  • Artificial Intelligence
  • Software Development
  • Web Application
  • Machine Learning
  • Desktop Application
  • Data Mining

First Python Program

Simply, we know that the python is easy to learn so that as compare to other languages python needs less no. of lines of code to print "Hello World".

Data Types in Python

The datatypes in python are as follows:

  • String : In Python, string is an immutable sequence data type. It is inside single, double, or triple quotes.

  • List : List is orederd collection of elements that are enclosed within [] brackets. Lists are mutable.

  • Tuple : Tuples are orederd collection of elements that are enclosed within () brackets. Tuples are immutable.

  • Dictionary : Dictionary is an unorederd collection of key-values pairs that are enclosed within {} brackets. Dictionary is mutable.

  • Set : Set is unorederdand unindexed collection of elements that are enclosed within {} brackets. Duplicates are not allowed in set.


Taking Input from the User

Before more learn about datatypes first we need to know how to input string, numbers in program.So let's do it.

Program:

Output:

Pyhton String

Python string is also immutable we cannot change it. Python, string is an immutable sequence data type. We cannot change it when it is declared. It is inside single, double, or triple quotes.

Program:

Output:

When we enter string in single, double and triple qoutes there is no differnce in the output. When we enter the single character Python takes it as String because Python dosen't support character.

Python List

List is an orederd collection of elements that are enclosed within [] brackets. Lists are mutable. List consist heterogenous elements.We can change it when it is declared.

Program:


Output:


Python list datatype can update using append(), pop(), reverse(), sort() functions perform on list.

Python Tuple

Tuple is an orederd collection of elements that are enclosed within () brackets. Tuples are immutable. They cannot be change when it is declared. Tuple also consist heterogeneous elements.

Program:


Output:


We can perform addition of two tuples using "+" operator also repeat the tuple using "*" operator.

Python Dictionary

Python dictionary is an unorederd collection of key-value pairs that are enclosed within {} brackets. In dictionary all the elements must in key-value pairs. The first declared part is key and second one is value. Dictionary is mutable.

Program:


Output:


Dictionary datatype is used to add record like marks of students, etc. As performed in the above program we can access keys and also values from the dictionary.

Python Set

Python set is an unorederd and unindexed collectiuon of elements that are enclosed within {} brackets. Duplicate elements are not allowed in set. We can add new elements in set.

Program:


Output:


In Python set we can perform adding, pop(), update(), union(), intersection() methods. In frozsenset we cannot update it because the frozenset is immutable.

Operators in Python

  • Arithmatic Operators
  • Comparison/Relational Operators
  • Assignments Operators
  • Identity Operators
  • Bitwise Operators
  • Membership Operators
  • Logical Operators

1.Arithmatic Operators

Arithmatical operators are used with numeric values to perform common mathematical operations and they are as follows:
Operator Name Example
+ Addition a+b
- Subtraction a-b
* Multiplcation a*b
/ Division a/b
// Floor Division a//b
** Exponentiation a**b
% Modulus a%b

2.Comparison\Relational Operators

Comparison operator is used to compare two values and it is also used program which contains mathematical expressions.It is also known as Relational Operators.
Operator Name Example
== Equal a==b
!= Not Equal a!=b
> Greater than a>b
<

Less than a >= Greater than or Equal to a>=b <= Less than or equal to a<=b

3.Assignment Operators

Assignment Operators are mostly used to assign values to the variable and mostly used in conditional statements like "if-else" statements,etc.
Operator Example Same as
+= x+=3 x=x+3
-= x-= x=x-3
*= x*=3 x=x*3
/= x/=3 x=x/3

4.Logical Operators

Logical operators are used combine conditional statements.
Operator Description Example
and Return true if both are true x>5 and x>10
or Returns true if one of the given is true x>5 or x>10
not Reverse the result, if true then returns False and if false then returns True not(x>5 and x>10)

5.Identity Operators

Identity operators are used to compare the objects, but the comparison is about they are equal or not.
Operator Description Example
is Return true if both the variables are the same object a is b
is not Return true if both the variables are not the same object a is not b

6.Membership Operators

Membership operators are used to check the given sequence or statement is present in an object.
Operator Description Example
in Return true if the given variable is present in the object a in b
in not Return true if the given variable is not present in the object a in not b

7.Bitwise Operators

Bitwise operators are used to compare binary numbers from the given object.
Operator Name DEscription
& AND Sets 1 when all given bits are 1
| OR Sets 1 if one of two bits are 1
^ XOR Sets 1 if only one of two bits are 1
~ NOT Inverts all the bits

4 Comments

Previous Post Next Post