Unit I
Unit I
USING PYTHON
Unit 1: History and basic
Python syntax
Interpreter improvements:
• Precise line numbers for debugging and other tools.
▶ Installing
▶ Double-click the icon labeling the file python-
3.10.1- amd64.exe.A Python 3.3.10.1 (64-bit)
Setup pop-up window will appear.
Cont..
▶ Ensure that the Install launcher for all users (recommended) and
the Add Python 3.10 to PATH checkboxes at the bottom are checked.
▶ If the Python Installer finds an earlier version of Python installed on
your computer, the Install Now message may instead appear as Upgrade
Now (and the checkboxes will not appear).
▶ Highlight the Install Now (or Upgrade Now) message, and then click it.
When run, a User Account Control pop-up window may appear on your
screen. I could not capture its image, but it asks, Do you want to allow
this app to make changes to your device.
▶ Click the Yes button. A new Python 3.10.1 (64-bit) Setup pop-up window
will appear with a Setup Progress message and a progress bar.
▶
Cont..
Cont..
▶ #!/usr/bin/python
▶ counter = 100 # An integer assignment
▶ miles = 1000.0 # A floating point
▶ name = "John" # A string
▶ print counter
▶ print miles
▶ print name
Cont..
Example
Python
Keywords
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass
Python Identifier Naming Rules
▶ Rules for naming Identifiers in Python
▶ So we know what a Python Identifier is. But can we name it
anything? Or do certain rules apply?
▶ Well, we do have five rules to follow when naming identifiers
in Python:
▶ a. A Python identifier can be a combination of lowercase/
uppercase letters, digits, or an underscore. The following
characters are valid:
▶ Lowercase letters (a to z)
▶ Uppercase letters (A to Z)
▶ Digits (0 to 9)
▶ Underscore (_)
Cont..
▶ Invoking the interpreter without passing a script file as a parameter brings up the
following prompt −
▶ $ python Python 2.4.3 (#1, Nov 11 2010, 13:34:43) [GCC 4.1.2 20080704 (Red Hat
4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more
information.
▶ >>>
▶ Type the following text at the Python prompt and press the Enter −
▶ If you are running new version of Python, then you would need to use print
statement with parenthesis as in print ("Hello, Python!");. However in Python
version 2.4.3, this produces the following result −
▶ Hello, Python!
Cont..
▶ We assume that you have Python interpreter set in PATH
variable. Now, try to run this program as follows −
▶ $ python test.py
▶ This produces the following result −
▶ Hello, Python! Let us try another way to execute a
Python script. Here is the modified test.py file −
▶ #!/usr/bin/python print "Hello, Python!―
▶ We assume that you have Python interpreter available
in /usr/bin directory. Now, try to run this program as
follows −
▶ $ chmod +x test.py # This is to make file executable
$./test.py
▶ This produces the following result −
▶ Hello, Python!
Comments in Python
▶ Comments are descriptions that help programmers
better understand the intent and functionality of the
program.
▶ They are completely ignored by the Python interpreter.
▶ Advantages of Using Comments
▶ Using comments in programs makes our code more
understandable. It makes the program more readable
which helps us remember why certain blocks of code
were written.
▶ Other than that, comments can also be used to ignore
some code while testing other blocks of code. This
offers a simple way to prevent the execution of some
lines or write a quick pseudo-code for the program.
▶
Single-Line Comments in
Python
▶ In Python, we use the hash symbol # to write a single-
line comment.
▶ Example 1: Writing Single-Line Comments
▶ # printing a string print('Hello world') Output
▶ Hello world
▶ Here, the comment is:
▶ # printing a string
Multi-Line Comments in
Python
▶ Python doesn't offer a separate way to write multiline
comments. However, there are other ways to get around this
issue.
▶ We can use # at the beginning of each line of comment on
multiple lines.
▶ Example 2: Using multiple #
▶ # it is a
▶ # multiline
▶ # comment
▶ Here, each line is treated as a single comment and all of them
are ignored.
Cont..
▶ Strings are amongst the most popular types in Python.
We can create them simply by enclosing characters in
quotes. Python treats single quotes the same as double
quotes. Creating strings is as simple as assigning a value
to a variable. For example −
▶ var1 = 'Hello World!'
▶ var2 = "Python Programming―
▶ Computers do not deal with characters, they deal with
numbers (binary). Even though you may see characters
on your screen, internally it is stored and manipulated
as a combination of 0s and 1s.
▶ This conversion of character to a number is called
encoding, and the reverse process is decoding. ASCII and
Unicode are some of the popular encodings used.
Cont..
[:] Range Slice - Gives the characters a[1:4] will give ell
from the given range
not in Membership - Returns true if a character does not M not in a will give 1
exist in the given string
r/R Raw String - Suppresses actual meaning of Escape print r'\n' prints \n and print R'\n'prints \n
characters. The syntax for raw strings is exactly
the same as for normal strings with the exception
of the raw string operator, the letter "r," which
precedes the quotation marks. The "r" can be
lowercase (r) or uppercase (R) and must be placed
immediately preceding the first quote mark.
▶ input ( prompt )
▶ raw_input ( prompt )