python Lecture 2
python Lecture 2
◼ 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.
◼ 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.
◼ 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
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
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
◼ >>> print("My","name","is",sep="_",end="*")
◼ My_name_is*
7
➢ Programming***Essentials***in...Python
Programming***Essentials***in...
Python
8
Scripting Mode
◼ The scripting mode is also called the "normal mode" (or the
"programming mode") and is non-interactive.
“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
11
variable naming rules
➢ Must begin with a letter (a - z, A - Z) or underscore (_)
➢ Other characters can be letters, numbers or _ only
12
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
Multiple Assignment
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.
▪ 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