Chapter (1)
Introduction to Python
What is Python?
Python is simple to use, but it is a real programming language, offering much more structure and
support for large programs than shell scripts or batch files can offer.
Python allows you to split your program into modules that can be reused in other Python programs.
It comes with a large collection of standard modules that you can use as the basis of your programs
— or as examples to start learning to program in Python. Some of these modules provide things like
file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.
Python is an interpreted language, which makes it easy to experiment with features of the language,
to test functions during bottom-up program development. It is also a handy desk calculator.
Python enables programs to be written compactly and readably. Programs written in Python are
typically much shorter than equivalent C, C++, or Java programs, for several reasons:
● the high-level data types allow you to express complex operations in a single statement;
● statement grouping is done by indentation instead of beginning and ending brackets;
● no variable or argument declarations are necessary.
IDLE - Python GUI
IDLE is Python’s Integrated Development and Learning Environment and is an alternative to the
command line. As the name may imply, IDLE is very useful for developing new code or learning
python. On Windows this comes with the Python interpreter, but in other operating systems you may
need to install it through your package manager.
The main purposes of IDLE are:
● Multi-window text editor with syntax highlighting, autocompletion, and smart indent
● Python shell with syntax highlighting
● Integrated debugger with stepping, persistent breakpoints, and call stack visibility
● Automatic indentation (useful for beginners learning about Python's indentation) Saving the
● Python program as .py files and run them and edit them later at any them using IDLE.
Open Python IDLE
If you have not installed python IDLE in your PC, you can download the latest version from
[Link] as following.
Python Programming Ch 1-1
KMD Computer Centre
● Click Start Button and then click IDLE (Python 3.11.2) to open Python 3.11.2 Shell.
● Open the Python Shell.
● For open the Python Code Editor, Click File menu and choose New File (Ctrl+N).
Python Programming Ch 1-2
KMD Computer Centre
Create Hello World Program
● In Python, print() is a built-in function used to display output to the console or terminal. It allows
you to print text, numbers, variables, or any other data to the standard output stream. The
general syntax of the print() function is:
● Type the following code in Python Code Editor.
● To save the file, click File menu and choose Save (Ctrl+S).
● When Save As Dialog box appear, choose Directory and assign the file name. (e.g.,
[Link])
● To run the program, click Run menu and choose Run Module (F5).
● Display the result as shown below.
Python Programming Ch 1-3
KMD Computer Centre
Python Escape sequence characters
Escape Sequence Description Example
' Prints single-quote print (" \' ")
" Prints double-quote print (" \" ")
\ Prints backslash print (" \\ ")
\n Linefeed print ("\n")
\t Prints horizontal tab (TAB) print ("\t")
Comments
● Single line comment: #
● Multi line comment: " " " (Three double quotation marks.)
Sample (1)
● Create New File. Type following code.
print ("Backslash & Forward Slash")
print (" \" \\ \" is the Backslash")
print (" \" / \" is the Forward Slash")
#print ("<&>")
“”” print (" \"<\" is less than Comparison Operator")
print (" \">\" is greater than Comparison Operator") “””
Sample (2)
print(5/2) #division operation that calculates the result of dividing, which always returns a
float.
print(5//2) #floor division operator, the result is the largest integer that is less than or equal
to the exact division result.
print("Hello World",end=' ') # Do not go to next line in next print
print('Hello\tWorld') #Tab between words
Python Programming Ch 1-4
KMD Computer Centre
print("Hello\nWorld") # to enter new line
print("Help!"*5) # String multiplication operation
print("\\"*10) #to print 10 times backslash
print('"') # print quotation mark double in single
print("'") # print quotation mark single in double
print(4) # print integer
print(4**2)#calculates 4 raised to the power of 2, which is equivalent to 4 multiplied by itself:
print(4>3) #The comparison operator used here is >,
#which returns True if the condition is satisfied and False otherwise.
print(1>0 and 4%2==0)
Exercise
[Link] the following result and save with the filename “[Link]”.
"Hello Students!"
'I am a teacher from SE Department of KMD Institute'
\To Teach Python Programming Language.
/* Thank You */
Python Programming Ch 1-5