Introduction to Python: History, Shell, Installation, and Usage

undefined
UNIT - 1
1)
History of Python
2)
 Python shell
3)
 Running Python Scripts
4)
 Variables
5)
 Assignment
6)
 Keywords
7)
 Input-Output
8)
 Indentation
1. History of Python
 
2. Python shell
Available(default) -Linux and Mac OS X.
Verify- open terminal and type
$ python
Download:      
http://www.python.org
Linux Installation
Open a Web browser and go to
http://www.python.org/download/.
Follow the link to download zipped source code
available for Unix/Linux.
Download and extract files.
Go to downloaded folder from terminal and run
./configure
make
make install
Installed path is 
/usr/local/bin
Windows Installation
 Open a Web browser and go to
http://www.python.org/download/
Download .exe based 32-bit or 64 bit
For  windows :       
python-3.5.2.exe
Just accept the default settings, wait until the
install is finished, and you are done.
Setting path
In Linux: 
Open a terminal and type
export PATH="$PATH:/usr/local/bin/python"
and press Enter
In Windows: 
At the command prompt, type
path %path%;C:\Python
 and press Enter.
Note: 
C:\Python is the path of the Python directory
PYTHONPATH: Tells the Python interpreter where to locate the
module files imported into a program.
PYTHONPATH is sometimes preset by the Python installer.
 
 
Running Python
From Unix, DOS, or any other system that provides
a command-line interpreter or shell window.
  
Linux: 1. open terminal window
   
2. type
     
$python
 
Windows: 1. open cmd window(command prompt)
   
2. type
     
>python
 
Script from the Command-line
Linux:
$python script.py
Windows
C:/>python script.py
Integrated Development Environment
Unix: 
IDLE is the very first Unix IDE for Python.
Windows: 
PythonWin is the first Windows interface
for Python and is an IDE with a GUI. (default)
Linux IDLE
Type the following command to install IDLE in Linux
$ sudo apt-get install idle3
After installing IDLE , type the following command in
terminal
$ IDLE3
Windows IDLE
 
Variables
Variables are nothing but reserved memory
locations to store values.
When we create a variable, we reserve some
space in memory.
Based on the data type of a variable, the
interpreter allocates memory.
By assigning different data types to variables, you
can store integers, decimals, or characters in these
variables
 
Python variables do not need explicit declaration to
reserve memory space.
 The declaration happens automatically when you
assign a value to a variable.
 The equal sign (=) is used to assign values to variables.
Rules:
Starts with a letter(A-Z or a-z) or an underscore( _ )
followed by any number of letters, numbers and
underscores
They are case sensitive
 
Ex : wordCount
Y_axis
Errorfiled
_logfile
_2
7index      
 
  (invalid)
Won’t_work         (invalid)
__name__
 
:system name
__name
 
:private class members
 
Ex:
 
age=24
   temp=97.4
 
sec=‘a’
 
c=7+8j
 
name=“xyz”
Variables demo
a=2
b=2.3
c="hello“
print(a)
print(b)
print(c)
#output:
2
2.3
hello
Assignment
Python allows you to assign a single value to several
variables simultaneously. For example:
a = b = c = 1
We can also assign multiple objects to multiple
variables. For example:
a, b, c = 1, 2, "john"
Keywords
and 
  
exec 
  
not 
  
assert
finally 
 
or 
  
break
 
 
 
for
pass 
  
class 
  
from 
  
print 
 
continue 
 
global 
 
raise 
  
def
if
  
 
 
return 
 
del 
  
import
try 
  
elif 
  
in 
  
while 
 
else 
  
is 
  
with 
  
except
lambda 
 
yield 
 
Input function
input(prompt=None,/)
 Read a string from standard input.
 The trailing newline is stripped.
  The prompt string, if given, is printed to standard
output without a trailing newline before reading
input.
  If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-
Z+Return), raise EOFError.
Input - function
>>> a=input()
42
>>> print(a)
42
>>> a=input("enter a value")
enter a value42
>>> print(a)
42
Output- print()
Prints the values to a  output window.
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  or
print(value1,value2,…)
file:  a file-like object (stream); defaults to the current
sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a
newline.
flush: whether to forcibly flush the stream.
 
>>>a=4
>>> b=6
>>> print(a,b)
4 6
>>>print(a,b,sep='\t')
>>>4
  
6
 
Create  a file in IDLE and save it as add.py
x = input("Enter an integer: ")
y = input("Enter another integer: ")
z=int(x)+int(y)
print("sum of ", x, " and ", y, " is ", z, ".", sep=" ")
Output
Enter an integer: 23
Enter another integer: 12
sum of  23  and  12  is  35 .
 
Create  a file in IDLE and save it as add.py
x = input("Enter an integer: ")
y = input("Enter another integer: ")
z=int(x)+int(y)
print("sum of {} and {} is {}".format(x,y,z))
Output
Enter an integer: 23
Enter another integer: 12
sum of  23  and  12  is  35 .
 
x = input("Enter an integer: ")
y = input("Enter another integer: ")
z=int(x)+int(y)
print("sum of %s and %s is %d" % (x,y,z))
Output
Enter an integer: 1
Enter another integer: 2
sum of 1 and 2 is 3
Indentation
Python does not support braces to indicate blocks
of code for class and function definitions or flow
control.
Blocks of code are denoted by line indentation.
All the continuous lines indented with same number
of spaces would form a block.
Python strictly follow indentation rules to indicate
the blocks.
 
Example 1:
if True:
    print("True")
else:
    print("False")
 
if True:
print
("True")
else:
print("False")
 
if True:
 
print("True")
else:
print
("False")
 
if True:
    print("True1")
        print("True2")
else:
    print("False")
 
if True:
        print("True1")
        print("True1")
else:
    print("False")
1. Numbers
Python has 4 built-in numeric data types:
1.
Integers
2.
Long integers
3.
Floating point numbers
4.
Imaginary numbers
1. Integers
Integers are whole numbers
They are 32- bit numbers
Range: -2147483648 to 2147483647
By default integers are base-10 numbers
>>>300
  
#300 in decimal
300
>>>0x12c
  
#300 in hex
300
>>>0o454
  
#300 in octal
300
2
. Long integers
Similar to integers, except that the maximum and
minimum values of long integers are restricted by
how much memory you have.
To differentiate between the two types of integers
append an “L” to the end of long integers.
(lowercase –l)
>>>200L
200L
3. Floating point numbers:
Represent fractional numeric values such as3.14159
They can represented in exponent form
They are rounded values
Ex:
200.05
9.80655
.1
2005e-2
6.0221367E23
4. Imaginary numbers
We form an imaginary number by appending a 
“j”
to the decimal number( int or float)
>>>2+5j
(2+5j)
>>>2*(2+5j)
4+10j
2. Strings
A contiguous set of characters represented in the
quotation marks.
Python allows for either pairs of single or double
quotes.
 Subsets of strings can be taken using the slice operator
([ ] and [:] ) with indexes starting at 0 in the beginning
of the string and working their way from -1 at the end.
The plus (+) sign is the string concatenation operator
and the asterisk (*) is the repetition operator
 
>>> c='aaaaaaaa bbbb cccc dddd eee ffff'
>>> c
'aaaaaaaa bbbb cccc dddd eee ffff‘
>>> st2="aaaaaaaa bbbb ccccc dddd eee ffff"
>>> st2
'aaaaaaaa bbbb ccccc dddd eee ffff‘
 
>>> st3="""aaaaa bbbbb
ccccc ddddd
eeeee fffff""“
>>> st3
'aaaaa bbbbb\nccccc ddddd\neeeee fffff'
>>>
 
str = 'Hello World!'
print(str)
print(str[0])
print(str[2:5])
print(str[2:])
print(str * 2)
print(str + "TEST")
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
3. Boolean
The identifiers True and False are interpreted as
Boolean values with the integer values of 0 and 1,
respectively.
In case of if, while – non zero values ->True
 
      Zero values 
 
->    False
>>> True
True
>>> False
False
 
>>> a=True
>>> a
True
>>> 2>3
False
>>> 2<7
True
Slide Note
Embed
Share

Learn about the history of Python, how to run Python scripts in the shell, basic concepts like variables and indentation, installation steps for both Linux and Windows, setting up paths, running Python on different systems, and using Integrated Development Environments (IDEs) like IDLE and PythonWin.

  • Python
  • History
  • Shell
  • Installation
  • Syntax
  • Variables

Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. UNIT - 1 History of Python Python shell Running Python Scripts Variables Assignment Keywords Input-Output Indentation 1) 2) 3) 4) 5) 6) 7) 8)

  2. 1. History of Python

  3. 2. Python shell Available(default) -Linux and Mac OS X. Verify- open terminal and type $ python Download: http://www.python.org

  4. Linux Installation Open a Web browser and go to http://www.python.org/download/. Follow the link to download zipped source code available for Unix/Linux. Download and extract files. Go to downloaded folder from terminal and run ./configure make make install Installed path is /usr/local/bin

  5. Windows Installation Open a Web browser and go to http://www.python.org/download/ Download .exe based 32-bit or 64 bit For windows : python-3.5.2.exe Just accept the default settings, wait until the install is finished, and you are done.

  6. Setting path In Linux: Open a terminal and type export PATH="$PATH:/usr/local/bin/python" and press Enter In Windows: At the command prompt, type path %path%;C:\Python and press Enter. Note: C:\Python is the path of the Python directory PYTHONPATH: Tells the Python interpreter where to locate the module files imported into a program. PYTHONPATH is sometimes preset by the Python installer.

  7. Running Python From Unix, DOS, or any other system that provides a command-line interpreter or shell window. Linux: 1. open terminal window 2. type $python Windows: 1. open cmd window(command prompt) 2. type >python

  8. Script from the Command-line Linux: $python script.py Windows C:/>python script.py

  9. Integrated Development Environment Unix: IDLE is the very first Unix IDE for Python. Windows: PythonWin is the first Windows interface for Python and is an IDE with a GUI. (default)

  10. Linux IDLE Type the following command to install IDLE in Linux $ sudo apt-get install idle3 After installing IDLE , type the following command in terminal $ IDLE3

  11. Windows IDLE

  12. Variables Variables are nothing but reserved memory locations to store values. When we create a variable, we reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory. By assigning different data types to variables, you can store integers, decimals, or characters in these variables

  13. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. Rules: Starts with a letter(A-Z or a-z) or an underscore( _ ) followed by any number of letters, numbers and underscores They are case sensitive

  14. Ex : wordCount Y_axis Errorfiled _logfile _2 7index Won t_work (invalid) (invalid) __name__ :system name __name :private class members

  15. Ex: age=24 temp=97.4 sec= a c=7+8j name= xyz

  16. Variables demo a=2 b=2.3 c="hello #output: 2 2.3 hello print(a) print(b) print(c)

  17. Assignment Python allows you to assign a single value to several variables simultaneously. For example: a = b = c = 1 We can also assign multiple objects to multiple variables. For example: a, b, c = 1, 2, "john"

  18. Keywords and finally pass continue if try else lambda exec or class global return elif is yield not break from raise del in with assert for print def import while except

  19. Input function input(prompt=None,/) Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl- Z+Return), raise EOFError.

  20. Input - function >>> a=input() 42 >>> print(a) 42 >>> a=input("enter a value") enter a value42 >>> print(a) 42

  21. Output- print() Prints the values to a output window. print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) or print(value1,value2, ) file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

  22. >>>a=4 >>> b=6 >>> print(a,b) 4 6 >>>print(a,b,sep='\t') >>>4 6

  23. Create a file in IDLE and save it as add.py x = input("Enter an integer: ") y = input("Enter another integer: ") z=int(x)+int(y) print("sum of ", x, " and ", y, " is ", z, ".", sep=" ") Output Enter an integer: 23 Enter another integer: 12 sum of 23 and 12 is 35 .

  24. Create a file in IDLE and save it as add.py x = input("Enter an integer: ") y = input("Enter another integer: ") z=int(x)+int(y) print("sum of {} and {} is {}".format(x,y,z)) Output Enter an integer: 23 Enter another integer: 12 sum of 23 and 12 is 35 .

  25. x = input("Enter an integer: ") y = input("Enter another integer: ") z=int(x)+int(y) print("sum of %s and %s is %d" % (x,y,z)) Output Enter an integer: 1 Enter another integer: 2 sum of 1 and 2 is 3

  26. Indentation Python does not support braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation. All the continuous lines indented with same number of spaces would form a block. Python strictly follow indentation rules to indicate the blocks.

  27. Example 1: if True: print("True") else: print("False")

  28. if True: print("True") else: print("False")

  29. if True: else: print("False") print("True")

  30. if True: print("True1") print("True2") else: print("False")

  31. if True: print("True1") print("True1") else: print("False")

  32. 1. Numbers Python has 4 built-in numeric data types: Integers Long integers Floating point numbers Imaginary numbers 1. 2. 3. 4.

  33. 1. Integers Integers are whole numbers They are 32- bit numbers Range: -2147483648 to 2147483647 By default integers are base-10 numbers >>>300 #300 in decimal 300 >>>0x12c #300 in hex 300 >>>0o454 #300 in octal 300

  34. 2. Long integers Similar to integers, except that the maximum and minimum values of long integers are restricted by how much memory you have. To differentiate between the two types of integers append an L to the end of long integers. (lowercase l) >>>200L 200L

  35. 3. Floating point numbers: Represent fractional numeric values such as3.14159 They can represented in exponent form They are rounded values Ex: 200.05 9.80655 .1 2005e-2 6.0221367E23

  36. 4. Imaginary numbers We form an imaginary number by appending a j to the decimal number( int or float) >>>2+5j (2+5j) >>>2*(2+5j) 4+10j

  37. 2. Strings A contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator

  38. >>> c='aaaaaaaa bbbb cccc dddd eee ffff' >>> c 'aaaaaaaa bbbb cccc dddd eee ffff >>> st2="aaaaaaaa bbbb ccccc dddd eee ffff" >>> st2 'aaaaaaaa bbbb ccccc dddd eee ffff

  39. >>> st3="""aaaaa bbbbb ccccc ddddd eeeee fffff"" >>> st3 'aaaaa bbbbb\nccccc ddddd\neeeee fffff' >>>

  40. Output: str = 'Hello World!' print(str) print(str[0]) print(str[2:5]) print(str[2:]) print(str * 2) print(str + "TEST") Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST

  41. 3. Boolean The identifiers True and False are interpreted as Boolean values with the integer values of 0 and 1, respectively. In case of if, while non zero values ->True Zero values >>> True True >>> False False -> False

  42. >>> a=True >>> a True >>> 2>3 False >>> 2<7 True

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#