0% found this document useful (0 votes)
103 views26 pages

Chapter 1

Uploaded by

vidyadevi6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views26 pages

Chapter 1

Uploaded by

vidyadevi6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CHAPTER 1

Introduction and Syntax


of Python programming

1
Features of Python
• Simple • Embeddable
• Easy to Learn • Extensive
• Versatile • Easy maintenance
• Free and Open Source • Secure
• High-level Language • Robust
• Interactive • Multi-threaded
• Portable • Garbage Collection
• Object Oriented
• Interpreted
• Dynamic
• Extensible 2
Applications of Python
• Embedded scripting language: Python is used as an embedded scripting language for
various testing/ building/ deployment/ monitoring frameworks, scientific apps, and
quick scripts.
• 3D Software: 3D software like Maya uses Python for automating small user tasks, or
for doing more complex integration such as talking to databases and asset
management systems.
• Web development: Python is an easily extensible language that provides good
integration with database and other web standards.
GUI-based desktop applications: Simple syntax, modular architecture, rich text
processing tools and the ability to work on multiple operating systems makes Python a
preferred choice for developing desktop-based applications.
• Image processing and graphic design applications: Python is used to make 2D 3
Applications of Python
• Scientific and computational applications: Features like high speed, productivity and
availability of tools, such as Scientific Python and Numeric Python, have made Python a
preferred language to perform computation and processing of scientific data. 3D
modeling software, such as FreeCAD, and finite element method software, like Abaqus,
are coded in Python.
Games: Python has various modules, libraries, and platforms that support development
of games. Games like Civilization-IV, Disney's Toontown Online, Vega Strike, etc. are
coded using Python.
• Enterprise and business applications: Simple and reliable syntax, modules and
libraries, extensibility, scalability together make Python a suitable coding language for
customizing larger applications. For example, Reddit which was originally written 4 in
Common Lips, was rewritten in Python in 2005. A large part of Youtube code is also
Writing and Executing First Python Program

Step 1: Open an editor.


Step 2: Write the instructions
Step 3: Save it as a file with the filename having the extension .py.
Step 4: Run the interpreter with the command python program_name.py or use IDLE
to run the programs.
To execute the program at the command prompt, simply change your working
directory to C:\Python34 (or move to the directory where you have saved Python)
then type python program_name.py.
If you want to execute the program in Python shell, then just press F5 key or click on
Run Menu and then select Run Module.
5
Modes in Python
Types of mode:
A. Interactive mode
B. Script Mode

A. Interactive Mode:
In the interactive mode as we enter a command and press enter, the very
next step we get the output. The output of the code in the interactive mode is
influenced by the last command we give. Interactive mode is very convenient for
writing very short lines of code.

print(“Hello”)

Disadvantages of Interactive mode:


• The interactive mode is not suitable for large programs.
• The interactive mode doesn’t save the statements. Once we make a program it is
for that time itself, we cannot use it in the future. In order to use it in the future, 6

we need to retype all the statements.


B. Script Mode:
In the script mode, a python program can be written in a file. This file can
then be saved and executed using the command prompt. We can view the code at
any time by opening the file and editing becomes quite easy as we can open and
view the entire code as many times as we want.

7
8
Literal Constants
The value of a literal constant can be used directly in programs. For example, 7, 3.9, 'A',
and "Hello" are literal constants.
Numbers refers to a numeric value. You can use four types of numbers in Python
program- integers, long integers, floating point and complex numbers.
• Numbers like 5 or other whole numbers are referred to as integers. Bigger whole
numbers are called long integers. For example, 535633629843L is a long integer.
• Numbers like are 3.23 and 91.5E-2 are termed as floating point numbers.
• Numbers of a + bi form (like -3 + 7i) are complex numbers.
Exampl
es:

9
Literal Constants
Strings
A string is a group of characters.
• Using Single Quotes ('): For example, a string can be written as 'HELLO'.
• Using Double Quotes ("): Strings in double quotes are exactly same as those in single
quotes. Therefore, 'HELLO' is same as "HELLO".
• Using Triple Quotes (''' '''): You can specify multi-line strings using triple quotes. You
can use as many single quotes and double quotes as you want in a string within triple
quotes.
Exampl
es:

10
print("Welcome to Python Programming
Language")
print('Welcome to Python Programming
Language')
print('''Welcome to Python
Programming Language''')

11
Escape Sequences
Some characters (like ", \) cannot be directly included in a string. Such characters must
be escaped by placing a backslash before them.

Exampl
e:

12
Raw Strings
If you want to specify a string that should not handle any escape sequences and want to
display exactly as specified then you need to specify that string as a raw string. A raw
string is specified by prefixing r or R to the string.

Exampl
e:

13
print("Hi\nHello")
print(r"Hi\nHello")
print("\n")

print("Hi\'Hello")
print(r"Hi\'Hello")
print("\n")

print("Hi\\Hello")
print(R"Hi\\Hello")
14
Variables and Identifiers
Variable means its value can vary. You can store any piece of information in a
variable. Variables are nothing but just parts of your computer’s memory where
information is stored. To be identified easily, each variable is given an appropriate
name.
Identifiers are names given to identify something. This something can be a variable,
function, class, module or other object. For naming any identifier, there are some
basic rules like:
• The first character of an identifier must be an underscore ('_') or a letter (upper or
lowercase).
• The rest of the identifier name can be underscores ('_'), letters (upper or
lowercase), or digits (0-9). 15

• Identifier names are case-sensitive. For example, myvar and myVar are not the
Assigning or Initializing Values to Variables
In Python, programmers need not explicitly declare variables to reserve memory space.
The declaration is done automatically when a value is assigned to the variable using the
equal sign (=). The operand on the left side of equal sign is the name of the variable and
the operand on its right side is the value to be stored in that variable.

Exampl
e:

16
s="Python"
a=100
b=10.5
c=5+3j
print("sting="+s)
print("int number="+str(a))
print("float number="+str(b))
print("complex number="+str(c))
print("\n\n")
a,b,c=100,10.5,5+3j
print("sting="+s)
print("int number="+str(a))
print("float number="+str(b))
print("complex number="+str(c))
print("\n\n")
a=b=c=0
print(a)
print(b)
print(c)
17
Comments

Comments are the non-executable statements in a program.


They are just added to describe the statements in the program
code. Comments make the program easily readable and
understandable by the programmer as well as other users who
are seeing the code. The interpreter simply ignores the
comments.

18
1. Single Line Comments: In Python for single line comments use # sign to
comment out everything following it on that line.
2. Multiple Lines Comments: Multiple lines comments are slightly different.
Simply use 3 single quotes before and after the part you want to be
commented.
Example:
#This is comment
#print("Statement will not be executed")
print("Statement will be excuted")

'''print("Multiple line comment")


print("Multiple line comment")
print("Multiple line comment")''' 19
Input Operation
To take input from the users, Python makes use of the input() function. The input()
function prompts the user to provide some information on which the program can
work and give the result. However, we must always remember that the input function
takes user’s input as a string.
Exampl
e:

20
a=input("Enter a number")
b=input("Enter a number")
print(a+b)

a=int(input("Enter a number"))
b=int(input("Enter a number"))
print(a+b)

21
a=float(input("Enter a number"))
b=float(input("Enter a number"))
print(a+b)

a=complex(input("Enter a number"))
b=complex(input("Enter a number"))
print(a+b)

22
Data Type
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

Text Type: str


Numeric int, float, complex
Types:
Sequence list, tuple, range
Types:
Mapping dict
Type:
Set Types: set, frozenset
Boolean Type: bool 23

Binary Types: bytes, bytearray, memoryview


Examples:
x = str("Hello World") str
x = int(20) int
x = float(20.5) float
x = complex(1j) complex

x = list(("apple", "banana", list


"cherry"))
x = tuple(("apple", "banana", tuple
"cherry"))
x = range(6) range

x = dict(name="John", dict
age=36)
x = set(("apple", "banana", "cherry")) set
x = frozenset(("apple", "banana", frozenset
"cherry"))
x = bool(5) bool 24
x = bytes(5) bytes

x = bytearray(5) bytearray

x = memoryview(bytes(5)) memoryview

25
Boolean Values
-In programming you often need to know if an expression is True or False.
-You can evaluate any expression in Python, and get one of two answers, True or False.
-When you compare two values, the expression is evaluated and Python returns the
Boolean answer:

print(10 > 9)
print(10 == 9)
print(10 < 9)

26

You might also like