Unit-1 Python PSG
Unit-1 Python PSG
Course Outcome (CO): Display message on screen using Python Script on IDE.
Total Marks:08
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.
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
• Python Identifiers
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.
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.
num = 100
str = "BeginnersBook"
print(num)
print(str)
x = y = z =99
print(x)
print(y)
print(z)
a, b, c =5, 6, 7
print(a)
print(b)
print(c)
x = 10
y = 20
print(x + y)
p = "Hello"
q = "World"
print(p + " " + q)
output:
30
Hello World
5. Comments
Multi-line comments
1. Open a web browser and navigate to the Downloads for Windows section of the official Python
website.
• 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.
To adjust the default installation options, choose Customize installation instead and proceed to the
following step.
4. Choose the optional installation features. Python works without these features, but adding them
improves the program's usability.
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.
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 second way is to use the GUI to verify the Python installation. Follow the steps below to run the
Python interpreter or IDLE:
3. The interpreter opens the command prompt and shows the following window:
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
List is an ordered collection of similar or different types of items separated by commas and enclosed
within brackets [ ].
For example,
Here, we have created a list named languages with 3 string values inside it.
To access items from a list, we use the index number (0, 1, 2 ...). For example,
languages = ["Swift", "Java", "Python"]
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.
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)
String is a sequence of characters represented by either single or double quotes. For example,
name = 'Python'
print(name)
Python
Python for beginners
print(capital_city)
Run Code
Output
print(type(True))
print(type(False))
print(type(true))
Output:
<class 'bool'>
<class 'bool'>