0% found this document useful (0 votes)
6 views

python Lecture 2

The document outlines key features of Python, highlighting its ease of learning, readability, and broad standard library. It explains how to use Python in both interactive and scripting modes, providing examples of commands and syntax. Additionally, it covers variables, naming rules, and the use of comments in Python code.

Uploaded by

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

python Lecture 2

The document outlines key features of Python, highlighting its ease of learning, readability, and broad standard library. It explains how to use Python in both interactive and scripting modes, providing examples of commands and syntax. Additionally, it covers variables, naming rules, and the use of comments in Python code.

Uploaded by

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

Python Features

◼ Easy-to-learn − Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.

◼ Easy-to-read − Python code is more clearly defined and visible to the eyes.

◼ Easy-to-maintain − Python's source code is fairly easy-to-maintain.

◼ A broad standard library − Python's bulk of the library is very portable and cross-
platform compatible on UNIX, Windows, and Macintosh.

◼ Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.

◼ Databases − Python provides interfaces to all major commercial databases.

◼ Scalable − Python provides a better structure and support for large programs than
shell scripting.
1
How to Use Python
◼ Python language by working with the Python interpreter

◼ Two common ways the Python interpreter can be used

1. Interactive mode
2. Scripting mode

2
◼ Interactive mode is used when an user wants to run one single line or one block of
code. It runs very quickly and gives the output instantly.
◼ Script Mode, on the other hand , is used when the user is working with more than one
single code or a block of code.

3
Interactive Mode
◼ To start the Python interpreter in interactive mode, type the command
python3 on the command-line, at the shell prompt on Linux, as shown
below.
$ python
Python 3.6.6 (default, Aug 12 2018, 20:37:26)
Type "help", "copyright", "credits" or "license" for more information.
>>>
❑ To start the Python interpreter in interactive mode, type the command python on the
command-line, at the shell prompt on Windows, as shown below.

4
After that, you can type commands and statements at the primary prompt. Some
examples:
>>> 1 + 5
6
>>> 2 * 5
10
>>> 25- 5
20

>>> print("Hello World!\n")


Hello World!
>>>

5
print
◼ Print() : prints the specified message to the screen, or other standard output
device.

◼ Syntax:
print ("Message”)
print (Expression)
◼ Prints the given text message or expression value on the console and
moves the cursor down to the next line.
print (Item1, Item2, ..., ItemN)
◼ Prints several messages and/or expressions on the same line.

◼ Examples:
print( "Hello, world!“)
age = 45
print ("You have", 65 - age, "years until retirement“)
Output:
Hello, world!
You have 20 years until retirement
6
◼ A string can represent characters by preceding them with a backslash.
◼ \t tab character
◼ \n new line character
◼ \" quotation mark character
◼ \\ backslash character

◼ Example: "Hello\tthere\nHow are you?“

>>> print("Hello\tthere\nHow are you?")


Hello there
How are you?

◼ >>> print("My","name","is",sep="_",end="*")
◼ My_name_is*

7
➢ Programming***Essentials***in...Python

print("Programming","Essentials","in", sep="***", end="... Python ")

Programming***Essentials***in...
Python

print("Programming","Essentials","in", sep="***", end="...\n")


print("Python")

8
Scripting Mode
◼ The scripting mode is also called the "normal mode" (or the
"programming mode") and is non-interactive.

◼ The Python interpreter a text file containing a Python program (also


called a Python script) as input, on the command-line, as follows:
$ python myfile.py

“myfile.py” is the name of the text file that contains the Python program.
Python source files are given the filename extension “.py”.

9
Variables
◼ we have seen values (integers, floats and strings). We can associate a
name to a value and access
>>> a = 5
>>> a
5
▪ we have assigned (associated) the name "a" to an integer object whose
value is 5.
▪ An object name is also referred to as a variable.
▪ The interpreter displays the value (5) of the object named "a".

10
◼ Every object has the following attributes

➢ An identity: This means an address in memory


➢ A type: Use the built-in function type()to check this. Possible
operations and values depend on the type of an object
➢ A value: The value of some object types can change (mutable)
whereas other types cannot (immutable)
>>> b = 57
>>> c = b
>>> c
57
>>> id(b)
1661170464
>>> id(c)

11
variable naming rules
➢ Must begin with a letter (a - z, A - Z) or underscore (_)
➢ Other characters can be letters, numbers or _ only

➢ Names are case sensitive

➢ Reserved words cannot be used as a variable name

➢ A name should be meaningful: “price” is better than “p”.

➢ For a multiple-word name, use either the underscore as the delimiter

(e.g., temp_var and interest_rate) or use camelCase capitalization (e.g.,


tempVar, TempVar, interestRate or InterestRate)
➢ Shorter meaningful names are better than longer ones.
>>> myVar = 'one sentence'
>>> myvar
Traceback (most recent call last):
File "<stdin>", line 1, in ?
myvar
NameError: name 'myvar' is not defined

12
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string

Multiple Assignment

>>> a=b=c=1 >>>a,b,c=1,2,”join”


>>> a
1
>>> b
1
>>> c
1

13
Python Comments
◼ Comments can be used to explain Python code.
◼ Comments can be used to make the code more readable.
◼ Comments can be used to prevent execution when testing code.

Ex: #print("Hello, World!")


print("Cheers, Mate!")

▪ Python does not really have a syntax for multi line comments.
▪ To add a multiline comment you could insert a # for each line

Note: Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it.
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
14

You might also like