Fundamentals of Python
Fundamentals of Python
Features of Python
Easy to Learn and Use - developer-friendly
Expressive Language - more understandable and readable
Interpreted Language – debugging is easy
Free and Open Source
Object-Oriented Language
Large Standard Library
GUI Programming Support
Integrated - It can be easily integrated with languages like
C, C++, JAVA etc.
Python Identifiers
A Python identifier is a name used to identify a variable,
function, class, module or other object.
An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
Python does not allow punctuation characters such as @,
$, and % within identifiers.
Python is a case sensitive programming language.
Thus, Power and power are two different identifiers in
Python.
Reserved Words
The following list shows some of the Python keywords.
These are reserved words and you cannot use them as
constant or variable or any other identifier names.
All the Python keywords contain lowercase letters only.
Continued…..
Python Variables
Variables are nothing but reserved memory locations to store
values.
When you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory.
By assigning different data types to variables, you can store integers,
decimals or characters in these variables.
Rules to follow while naming the variables.
• Variable names can contain letters, numbers and the underscore.
• Variable names cannot contain spaces.
• Variable names cannot start with a number.
• Case matters—for instance, temp and Temp are different.
Comments in Python
Comments are very important while writing a program.
It describes what's going on inside a program so that a
person can understand it very easily.
In Python, we use the hash (#) symbol to start writing a
comment.
Python Interpreter ignores comment.
Multiline Comments
If we have comments that extend multiple lines, one way
of doing it is to use hash (#) in the beginning of each line.
For example:
Indentation in Python
Python does not provide braces to indicate blocks of code for
class and function definitions or flow control.
Blocks of code are denoted by line indentation.
The number of spaces in the indentation is variable, but all
statements within the block must be indented the same
amount.
for i in range(height):
if(i==0):
print(‘Height is 0’)
print('*'*width)
else:
if(i<height-1):
print('*')
else:
print('*'*width)
Input Statement
The input function is a simple way to get input from user.
name = input('Enter your name: ')
print('Hello, ', name)
The basic structure of input statement is
variable_name = input(message to user)
The above statement works for getting text from the user.
To get numbers from the user we can use ‘int’
num = int(input('Enter a number: '))
print('Your number squared:', num*num)
The ‘int’ function converts the number entered by the user
into an integer number.
Output Statement
We use the print() function to output data to the
standard output device.
print(1,2,3,4)
Output: 1 2 3 4
print(3+4)
Output: 7
print(‘3+4’)
Output: 3+4
Optional Arguments
1. Sep – Python has an optional argument called ‘sep’ that
can be used to change the space between arguments to
something else.
For example, using sep=':' would separate the arguments
by a colon and sep='##‘ would separate the arguments by
two pound signs.
One particularly useful possibility is to have nothing inside
the quotes, as in sep=''.
This says to put no separation between the arguments.
Continued…..
print(1,2,3,4,sep='*')
Output: 1*2*3*4
print(1,2,3,4,sep='#',end='&‘)
Output: 1#2#3#4&
Continued…..
2. end - There is an optional argument called ‘end’ that you
can use to keep the print function from advancing to the
next line.
Result will be –