Python M1 Ktunotes - in
Python M1 Ktunotes - in
Python - Programming
Python Programming Language
• Python is a high level language
• Introduced in 1991 by Guido Van Rossum, a Dutch computer programmer
• Interpreted Language
Advantages of Python
• Considerably less number of lines of code
• Simplest syntax, availability of library and built-in modules
• Extensive collection of third party resources
• Cross platform language
• Desktop application, database application, network programming, game programming, mobile
development, machine learning
Installing Python in your PC
• Presently the version is Python 3.x
• http://www.python.org/downloads
• Download the suitable version based on OS (Windows/Linux etc) and processor(32 bit/64bit)
• Download and install python interpreter
Interactive versus Script
• Interactive
• Command-line mode
• You type directly to Python one line at a time and it responds
• Script
You enter a sequence of statements (lines) into a file using a text editor and tell Python to execute the
statements in the file
Constants
• Fixed values such as integers, letters and strings are called constants because their values does not change
• Numeric constants
• String constants use single quotes(‘) or double quotes(“)
Variables
• A variable is a named place in memory where a programmer can store the data and later retrieve the data
using the variable name
• Programmers get to choose the names of the variable
• You can change the contents of a variable in a later statement
Variable Name Rules
• Must start with a letter or underscore_
• Must consist of letters, numbers and underscores
• Case sensitive
• Good: spam, egg, spam23, _egg
• Bad: 23spam, #sign, var.12
• Different: spam, SPAM, Spam
Reserved words
• You cannot use reserved words as variable names / identifiers
And except lambda with
As finally nonlocal while
Assert false None yield
Break for not
if statement example
userInput = input("Enter 1 or 2: ")
if userInput == "1":
print("Hello Python")
print("How are you")
elif userInput == "2":
print("Python is good")
print("I love Python")
else:
print("Invalid Number")
• The elif keyword is python’s way of saying "if the previous conditions were not true, then try this
condition".
• The else keyword catches anything which isn't caught by the preceding conditions
• The elif and else clauses are optional.
• Python does not have a switch statement, so you must use if, elif, and else for all conditional processing.
Python Loops
• Python has two primitive loop commands:
• while loops
• for loops
While statement
• while statement supports repeated execution of a statement or block of statements that is controlled by a
conditional expression
• With the while loop we can execute a set of statements as long as a condition is true.
while(expression):
statement1
statement2