UNIT-I Introduction and Syntax of Python Program
UNIT-I Introduction and Syntax of Python Program
2. Interpreted Language:
- Python is an interpreted language i.e. interpreter executes the code line by line at a time.
- This makes debugging easy.
- Python code is processed at runtime by the interpreter.
- You do not need to compile your program before executing it. This is similar to PERL and
PHP language.
- Interpreter generate the code which run on any platform that’s via Python is known as
Platform Independent programming language.
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-I Introduction and Syntax of Python Program
8. Integrated Language:
- Python is an Integrated programming language because we can easily integrate python with
other language like c, c++ etc.
9. High-Level Language:
- Python is a high-level language.
- When we write programs in python, we do not need to remember the system architecture,
nor do we need to manage the memory.
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-I Introduction and Syntax of Python Program
History of Python:
- Python laid its foundation in the late 1980s.
- The implementation of Python was started in the December 1989 by Guido Van Rossum at
Centrum Wiskunde & Informatica (CWI) in Netherland.
- In 1994, Python 1.0 was released with features like: lambda, map, filter, and reduce.
- Python 2.0 added new features like: list comprehensions, garbage collection system.
- On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to
rectify fundamental flaw of the language.
- ABC programming language is said to be the predecessor of Python language which was
capable of Exception Handling and interfacing with Amoeba Operating System.
Applications of Python:
Python is known for its general purpose nature that makes it applicable in almost each domain
of software development. Python programming is widely used in Artificial Intelligence, Natural
Language Generation, Neural Networks and other advanced fields of Computer Science.
1) Web Applications.
2) Desktop GUI Applications
3) Software Development
4) Scientific and Numeric
5) Business Applications
6) Console Based Application
7) Audio or Video based Applications
8) 3D CAD Applications
9) Enterprise Applications
10) Applications for Images
Many large companies use the Python programming language include NASA, Google, YouTube,
BitTorrent, etc.
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-I Introduction and Syntax of Python Program
Python Keywords:
- Keywords are the reserved words in Python.
- We cannot use keyword as a variable name, function name or some other purpose.
- Each and every keywords has some special meaning.
- In Python, keywords are case sensitive.
- All the keywords should be written in lower case except True, False and None.
- The list of all the keywords are given below. This list can vary slightly in the course of time.
Keywords in Python
as elif if or yield
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-I Introduction and Syntax of Python Program
Python Identifiers:
- An identifier is a name which given to class, functions, variables, etc.
- It helps to differentiate one entity from another.
- All user defined name which we use in Python code is known as Identifier. We use name for
the identification purpose.
- Following are the rules which we use while constructing the Identifier Name.
1. Identifiers name must start with a alphabets or the underscore(_) character.
2. It may combine with Numbers(0-9).
3. Identifiers name should not start with Numbers(0-9).
4. Keywords cannot be used as an Identifier name.
5. It does not allowed any white space between Identifiers name.
6. It does not allowed any special symbols except underscore in Identifiers name.
7. Identifiers names are case-sensitive (Eg. VJTECH, VJTech and vjtech are three different
variables).
8. Identifier name can be of any length.
Example:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-I Introduction and Syntax of Python Program
Python Variables:
- Variable is an Identifier which can change value during the execution of program.
- Variable act as a container for storing the value.
- A variable is a named location used to store data in the memory.
- In python, there is no need of declaration of variables by using the data type.
- We can directly use the variable name for storing the value and its data type is decided
dynamically as per the stored value on it.
- Following are the rules which we use while constructing the variable Name.
1. Variable name must start with a alphabets or the underscore(_) character.
2. It may combine with Numbers(0-9).
3. Variable name should not start with Numbers(0-9).
4. Keywords cannot be used as an Variable name.
5. It does not allowed any white space between Variable name.
6. It does not allowed any special symbols except underscore in Variable name.
7. Variable name is case-sensitive (Eg. VJTECH, VJTech and vjtech are three different
variables).
8. Variable name can be of any length.
Example:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-I Introduction and Syntax of Python Program
- Assigning a value to a Variable in Python: you can use the assignment operator (=) to assign
a value to a variable.
- Example 1: Declaring and assigning a value to a variable
Roll_No = 1010;
print("My Roll No:",Roll_No);
My Roll No:1010
Roll_No = 1010;
print("My Roll No:",Roll_No);
Roll_No = 2020;
print("My Roll No:",Roll_No);
My Roll No:1010
My Roll No:2020
print("Value of a=",a);
print("Value of b=",b);
print("Value of c=",c);
Value of a=56
Value of a=3.2
Value of a=Hello
a=b=c="Hello";
print("Value of a=",a);
print("Value of b=",b);
print("Value of c=",c);
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-I Introduction and Syntax of Python Program
Python Indentation:
- Most of the programming languages like C, C++, Java use curly brackets { } to define a block
of code. Python uses indentation.
- A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line.
- The amount of indentation is up to you, but it must be consistent throughout that block.
- If we use indentation in Python then the code look neat and clean.
- Generally four whitespaces are used for indentation or we can use single Tab.
- Example:
for i in range(1,11):
if i == 5:
break
print(i)
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-I Introduction and Syntax of Python Program
Python Comments:
- Comments are very important while writing a program.
- It describes what's going on inside a program so that a person looking at the source code
can understand the code easily.
- You might forget the key details of the program you just wrote in a mon th's time. So taking
time to explain these concepts in form of comments is always helpful.
- Comment is a part of documentation which helps us to give more detail information about
the code.
- Python Interpreter ignores the comments, it will not run.
- There are two types of comments present in Python.
1. Single line comments
2. Multi-line comments
- In Python, we use the hash (#) symbol to write a single line comment.
- Single line comment begin with # symbol and end with the end of line.
- We can cover multiple lines by using single line comment, for that you have to write #
symbol for beginning of each line.
- Example:
Multi-line Comments:
- In Python, multi-line comments begin with three times either single quotes (''') or double
quotes (""") and end with three times either single quotes (''') or double quotes (""").
- These triple quotes are generally used for multi-line strings.
- But they can be used as multi-line comment as well.
- Example:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-I Introduction and Syntax of Python Program
a=10
b=12.45
c="VJTech"
print("Value of a=",a," and data type is",type(a));
print("Value of b=",b," and data type is",type(b));
print("Value of c=",c," and data type is",type(c));
When you run the above program, the output will be:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-I Introduction and Syntax of Python Program
Integer (int):
a=10
print("Value of a=",a," and data type is",type(a));
a=10.45
b=12e4
print("Value of a=",a," and data type is",type(a));
print("Value of b=",b," and data type is",type(b));
b=4+3.4j
print("Value of b=",b," and data type is",type(b));
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-I Introduction and Syntax of Python Program
Python Strings:
- The collections of characters is known as String. Characters may be alphabets, numbers or
special symbols.
- String should be represented by using single quotes or double quotes.
- For String data type,predefined class str is used.
- Positive and Negative index associated with the String value.
- String positive index should begin with 0 and end with SIZE-1.
- Negative index should begin with -1 from the last element.
- Multi-line strings can be denoted using triple quotes, ''' or """.
- When we use plus (+) sign with String then it work as concatenation operator and when we
use asterisk(*) sign with String then it work as repetition operator.
- Strings are immutable. It means once it is created, you can not change it later.
- Subscript [] and index number is used to access any particular element from the string.
- We can use slice [:] operators to access the data of the strings.
- Example:
str1="VJTech Academy";
print(str1);
print(str1[:]);
print(str1[7:]);
print(str1[2:6]);
print(str1+" Awasari");
print(str1*2);
When you run the above program, the output will be:
VJTech Academy
VJTech Academy
Academy
Tech
VJTech Academy Awasari
VJTech AcademyVJTech Academy
Python List:
- List is an ordered sequence of items.
- It is one of the most used datatype in Python and is very flexible.
- All the items in a list do not need to be of the same type.
- List is a collection of mixed data types of items.
- List should be represented by using square bracket [ ].
- For List data type,predefined class list is used.
- Positive and Negative index associated with the list items.
- List positive index should begin with 0 and end with SIZE-1.
- Negative index should begin with -1 from the last element.
- Lists are mutable. It means once it is created, you can change it later.
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-I Introduction and Syntax of Python Program
- Subscript [] and index number is used to access any particular element from the list.
- We can use slice [:] operator to access the items of the list.
- Example:
a = [5,10,15,20,25,30,35,40]
print(a)
print(a[2])
print(a[0:3])
print(a[5:])
When you run the above program, the output will be:
Python Tuple:
- Tuple is an ordered sequence of items.
- All the items in a Tuple do not need to be of the same type.
- Tuple is a collection of mixed data types of items.
- Tuple should be represented by using parentheses ().
- For Tuple data type,predefined class tuple is used.
- Positive and Negative index associated with the Tuple items.
- Tuple positive index should begin with 0 and end with SIZE-1.
- Tuple index should begin with -1 from the last element.
- Tuple are immutable. It means once it is created, you can not change it later.
- Subscript [] and index number is used to access any particular element from the Tuple.
- We can use slice [:] operator to access the items of the Tuple
- Tuples are used to read only data and it is faster than list as it cannot change dynamically..
- Example:
a = (5,10,15,20,25,30,35,40)
print(a)
print(a[2])
print(a[0:3])
print(a[5:])
When you run the above program, the output will be:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-I Introduction and Syntax of Python Program
Python Set:
-Set is an unordered collection of unique items.
-Set is defined by values separated by comma inside braces { }.
-Items in a set are not ordered.
-We can perform set operations like union, intersection on two sets.
-Set have unique values. They eliminate duplicates.
- Set are unordered collection of items and index numbers are not associated with it. Hence the
slicing operator [:] does not work.
- Example:
a = {5,10,15,20,25,30,35,40}
print(a)
When you run the above program, the output will be:
Python Dictionary:
- Dictionary is an ordered collection of items and it should be represented by using (key:value)
pairs format.
- It is generally used when we have a huge amount of data.
- Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.
- In Python, dictionaries are defined by using curly bracket {} with each item being a pair in the
form key:value.
- Key and value can be of any data type.
- Example:
When you run the above program, the output will be:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-I Introduction and Syntax of Python Program
- The type casting which is done by system is known as Implicit type conversion.
- Python automatically convert one type value to another type.
- It does not require any user involvement.
- Example:
a = 123
b = 1.23
c = a + b
print("datatype of a:",type(a))
print("datatype of b:",type(b))
print("Value of c:",c)
print("datatype of c:",type(c))
When you run the above program, the output will be:
- The type casting which is done by programmer is known as Explicit type conversion.
- Here Programmer will convert the data type of an object to required data type.
- We use the predefined functions like int(), float(), str(), etc to perform explicit type
conversion..
- Syntax:
Variable_Name = (required_datatype) (Expression);
- Example:
a = 123
b = 1.23
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-I Introduction and Syntax of Python Program
c = (float)(a + b)
print("datatype of a:",type(a))
print("datatype of b:",type(b))
print("Value of c:",c)
print("datatype of c:",type(c))
When you run the above program, the output will be:
Where:
objects -> Value to be printed.
sep -> The separator is used between the values. It’s default value is a space character.
end -> After all values are printed then end is printed. It’s default value is a new line.
file -> The file is the object where the values are printed and its default value is
sys.stdout (screen)
- Example:
print(1,2,3,4)
print(1,2,3,4,sep='*')
print(1,2,3,4,sep='#',end='&')
When you run the above program, the output will be:
1 2 3 4
1*2*3*4
1#2#3#4&
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-I Introduction and Syntax of Python Program
Where:
message -> This is the string which we wish to display on the screen. It is optional.
- Example:
no=input("Enter any Value: ")
print("You have entered value of no= ",no);
When you run the above program, the output will be:
Python Programming by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17