You are on page 1of 18

Unit 1: Introduction and syntax of python programming

Course Outcome (CO): Display message on screen using Python Script on IDE.
Total Marks:08

P.S.Gaidhani Unit-1 Notes


Q.What is Python?

Python is an interpreted ,Object Oriented ,High Level Programming Language


with Dymanic Semantics

code compressed
Ex.
a=10
b=10
print(a+b)

Python is developed by Guido van Rossum. Guido van Rossum started implementing
Python in 1989.
Q. List and Explain Features of python

Interactive •You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs

Object Oriented •Python supports object oriented language and concepts of classes
and objects come into existence

•Interpreter reads the code line by line. It will stop at the point of error occurs
Interpreted •Ex. We have 3 lines of code .If interpreter finds the error at 2nd line ,Interpreter
gives the output of first line and will generate error at line 2

•Python can run equally on different platforms such as Windows, Linux, Unix and
Platform Independent Macintosh etc. So, we can say that Python is a portable language.

Easy to learn •Python language is easy to learn

Open Source •Python is freely available

Library •Broad standard Libraries OR rich Libary support

Multi Paradigm Language •We can use python as structural way,object Oriented Way or
Simple Programming Language way
High Level Programming •Easily understandable by the user
Labguage

Extendable Language •We can combine the python with other language and run it

GUI Programming Support •We can use it for desktopn applications

P.S.Gaidhani Unit-1 Notes


Q. List the applications of Python
1. Network Programming
2. Data Analysis
3. Robotics
4. Website and Application development
5. Desktop Application
6. Games Development
7. Data Visualization
8. Scientific Calculation
9. Machine Learning and Artificial Intelligence
10. 3D Applications
11. Audio and Video Software Development
12. IOT
13. Data base development
14. Big Data

Q. List and Explain Python building blocks

• Python Identifiers

Variable name is known as identifier.


• Python identifier is a name given to variable, function, class module or other object that
are going to use in python program

The rules to name an identifier are given below.

o The first character of the variable must be an alphabet or underscore ( _ ).


o All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore or digit (0-9).
o Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &,
*).
o Identifier name must not be similar to any keyword defined in the language.
o Identifier names are case sensitive for example my name, and MyName is not the
same.
o Examples of valid identifiers : a123, _n, n_9, etc.
o Examples of invalid identifiers: 1a, n%4, n 9, etc.

P.S.Gaidhani Unit-1 Notes


Assigning single value to multiple variables Eg:
x=y=z=50
Assigning multiple values to multiple variables:
Eg: a,b,c=5,10,15

2.Reserved Words
The following list shows the Python keywords. These are reserved words and cannot use them as
constant or variable or any other identifier names. All the Python keywords contain lowercase
letters only.

P.S.Gaidhani Unit-1 Notes


3. Indentation

Python provides no braces({}) to indicate blocks of code for class and function definitions or
flow control. Blocks of code are denoted by line indentation, which is compulsory.
The number of spaces in the indentation is variable, but all statements within the block must
be indented the same amount. For example −
if True:
print "True"
else:
print "False"

Thus, in Python all the continuous lines indented with same number of spaces would form a
block.

4. Variable Types

• Variables are used to store data, they take memory space based on the type of value we
assigning to them. Creating variables in Python is simple,
• you just have write the variable name on the left side of = and the value on the right side.

Python Variable Example

num = 100
str = "BeginnersBook"
print(num)
print(str)

Multiple Assignment Examples

We can assign multiple variables in a single statement like this in Python.

x = y = z =99
print(x)
print(y)
print(z)

Another example of multiple assignments

a, b, c =5, 6, 7
print(a)
print(b)
print(c)

P.S.Gaidhani Unit-1 Notes


Plus and concatenation operation on the variables

x = 10
y = 20
print(x + y)

p = "Hello"
q = "World"
print(p + " " + q)

output:
30
Hello World

5. Comments

Use the hash (#) symbol to start writing a comment.


1. #This is a comment
2. #print out Hello
3. print('Hello')

Multi-line comments

use triple quotes, either ''' or """. eg:


1. """This is also a
2. perfect example of
3. multi-line comments"""

Q. Define Data Types in Python

Q Python Environment Setup-Installation and Working Of IDE


Install Python on any operating system such as Windows, Mac OS X, Linux/Unix and others.
Step 1: Select Python Version
Deciding on a version depends on what you want to do in Python. The two major versions are
Python 2 and Python 3
Step 2: Download Python Executable Installer

Start by downloading the Python executable installer for Windows:

1. Open a web browser and navigate to the Downloads for Windows section of the official Python
website.

2. Locate the desired Python version.

P.S.Gaidhani Unit-1 Notes


3. Click the link to download the file. Choose either the Windows 32-bit or 64-bit installer.

The download is approximately 25MB.

Step 3: Run Executable Installer

The steps below guide you through the installation process:

1. Run the downloaded Python Installer.

2. The installation window shows two checkboxes:

• Admin privileges. The parameter controls whether to install Python for the current or all system
users. This option allows you to change the installation folder for Python.
• Add Python to PATH. The second option places the executable in the PATH variable after
installation. You can also add Python to the PATH environment variable manually later.

P.S.Gaidhani Unit-1 Notes


3. Select the Install Now option for the recommended installation (in that case, skip the next two
steps).

To adjust the default installation options, choose Customize installation instead and proceed to the
following step.

P.S.Gaidhani Unit-1 Notes


The default installation installs Python
to C:\Users\[user]\AppData\Local\Programs\Python\Python[version] for the current user. It
includes IDLE (the default Python editor),

4. Choose the optional installation features. Python works without these features, but adding them
improves the program's usability.

5. The second part of customizing the installation includes advanced options.

P.S.Gaidhani Unit-1 Notes


After picking the appropriate options, click Install to start the installation.

6. Select whether to disable the path length limit. Choosing this option will allow Python to bypass
the 260-character MAX_PATH limit.

After picking the appropriate options, click Install to start the installation.

P.S.Gaidhani Unit-1 Notes


6. Select whether to disable the path length limit. Choosing this option will allow Python to bypass
the 260-character MAX_PATH limit.

To install the Python on your operating system, go to this


link: https://www.python.org/downloads/. You will see a screen like this.

Step4: Verify Python Was Installed on Windows

The first way to verify that Python was installed successfully is through the command line. Open the
command prompt and run the following command:

python --version

The output shows the installed Python version.

The second way is to use the GUI to verify the Python installation. Follow the steps below to run the
Python interpreter or IDLE:

1. Navigate to the directory where Python was installed on the system.

P.S.Gaidhani Unit-1 Notes


2. Double-click python.exe (the Python interpreter) or IDLE.

3. The interpreter opens the command prompt and shows the following window:

Running IDLE opens Python's built-in IDE:

P.S.Gaidhani Unit-1 Notes


Q. Explain Python data types

Python is a dynamically-typed language, which means that the type of a variable is


interpreted at runtime. Here are some of the basic data types in Python:

The data types in Python are divided in two categories:


1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed

Immutable data types in Python are:


1. Numbers
2. String
3. Tuple

Mutable data types in Python are:


1. List
2. Dictionaries
3. Sets

P.S.Gaidhani Unit-1 Notes


1. Python Numeric Data type

In Python, numeric data type is used to hold numeric values.

Integers, floating-point numbers and complex numbers fall under Python numbers category. They
are defined as int, float and complex classes in Python.
• int - holds signed integers of non-limited length.
• float - holds floating decimal points and it's accurate up to 15 decimal places.
• complex - holds complex numbers.
We can use the type() function to know which class a variable or a value belongs to.
Let's see an example,

num1 = 5
print(num1, 'is of type', type(num1))

num2 = 2.0
print(num2, 'is of type', type(num2))

num3 = 1+2j
print(num3, 'is of type', type(num3))
Run Code
Output

5 is of type <class 'int'>


2.0 is of type <class 'float'>
(1+2j) is of type <class 'complex'>

2. Sequence Data Type


1. Python List Data Type

List is an ordered collection of similar or different types of items separated by commas and enclosed
within brackets [ ].
For example,

languages = ["Swift", "Java", "Python"]

Here, we have created a list named languages with 3 string values inside it.

P.S.Gaidhani Unit-1 Notes


Access List Items

To access items from a list, we use the index number (0, 1, 2 ...). For example,
languages = ["Swift", "Java", "Python"]

# access element at index 0


print(languages[0]) # Swift

# access element at index 2


print(languages[2]) # Python

2. Python Tuple Data Type

Tuple is an ordered sequence of items same as a list. The only difference is that tuples are
immutable. Tuples once created cannot be modified.

In Python, we use the parentheses () to store items of a tuple. For example,

product = ('Xbox', 499.99)

Here, product is a tuple with a string value Xbox and integer value 499.99.
Access Tuple Items

Similar to lists, we use the index number to access tuple items in Python . For example,

# create a tuple
product = ('Microsoft', 'Xbox', 499.99)

# access element at index 0


print(product[0]) # Microsoft

# access element at index 1


print(product[1]) # Xbox

P.S.Gaidhani Unit-1 Notes


3. Python String Data Type

String is a sequence of characters represented by either single or double quotes. For example,

name = 'Python'
print(name)

message = 'Python for beginners'


print(message)
Run Code
Output

Python
Python for beginners

3. Python Set Data Type


Set is an unordered collection of unique items. Set is defined by values separated by commas inside
braces { }.
For example,
# create a set named student_id
student_id = {112, 114, 116, 118, 115}

# display student_id elements


print(student_id)

# display type of student_id


print(type(student_id))
Run Code
Output

{112, 114, 115, 116, 118}


<class 'set'>

P.S.Gaidhani Unit-1 Notes


4. Python Dictionary Data Type
Python dictionary is an ordered collection of items. It stores elements in key/value pairs.
Here, keys are unique identifiers that are associated with each value.
Let's see an example,
# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

print(capital_city)
Run Code
Output

{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

5. Boolean Data Type in Python


Data type with one of the two built-in values, True or False.

print(type(True))
print(type(False))
print(type(true))

Output:
<class 'bool'>
<class 'bool'>

P.S.Gaidhani Unit-1 Notes


Assignment No:1

Q1. Questions for 2 Marks


1. Explain numeric data types in Python.
2. Differentiate between interactive mode and script mode of Python.
3. List different features supported by Python.
4. Describe Python Interpreter.
5. Enlist applications for Python programming

Q2. Questions for 4 Marks


1. Write steps to install Python and to run Python code.
2. List and Explain different data types used in Python.
3. Describe the role of indentation in Python.
4. Define the following terms: i. Identifier ii. Literal iii. Keyword iv. Variable
5. What do you mean by variable? Does Python allow explicit declaration of
variables? Justify your answer.

P.S.Gaidhani Unit-1 Notes

You might also like