Basics of Python Programming
Basics of Python Programming
Python Programming
Outline
Token
Token
Token
• Keyword
• Keywords are the reserved words in Python. Keywords cannot be used
as variable name, function name or any other identifier.
• There are 33 keywords in Python 3.4.2. This number can vary slightly in
course of time.
Token
• Identifiers
• Identifier is the name given to entities like class, functions, variables
etc. in Python. It helps differentiating one entity from another.
• Rules for writing identifiers
• Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names
like myClass, var_1 and print_this_to_screen, all are valid example.
• An identifier cannot start with a digit.1variable is invalid,
but variable1 is perfectly fine.
• Keywords cannot be used as identifiers.
• You cannot use special symbols like !, @, #, $, % etc. as an identifier.
• Identifier can be of any length.
Token
• Literals
• Literals are numbers or strings or characters that appear directly in
a program. A list of some literals in python is as follows:
• 70 # Integer Literal
• 21.80 # Floating Point Literal
• 5.34j # Complex Literals
• True/ False # Boolean Literals
• “Hello” # String Literals
Token
• Type of Literals
• To know the exact type of any value, python offers an in built method
called type.
• The syntax to know the type of any value is type(value)
Token
• Delimiters
• Delimiters are symbols that perform three special roles in Python:
grouping, punctuation, and assignment/binding of objects to names.
Token
• Operators
• Operators are particular symbols which operate on some values and
produce an output. The values are known as Operands. Operators
compute a result based on the value(s) of their operands: e.g., + is the
addition operator.
• Integer
• Type : int
• It includes following types of integer specifications:
o Normal integers i.e 18
o Octal literals (base 8)
o an octal number can be defined with “0o“(Zero and alphabet o in
upper or lower case) as a prefix i.e 0o12 or 0O12
o Hexadecimal literals (base 16)
o Hexadecimal literals have to be prefixed either by “0x" or “0X".
o i.e. 0x33 or 0X33
• Integer
• Type : int
• Use the type() function to know which class a variable or a value belongs
• Floating-point numbers
• e.g. 25.2
• Type : float
• Use the type() function to know which class a variable or a value belongs
Floating Point Number Notation Meaning
234.0 2.34e2 2.34* 102
0.234 2.34e-1 2.34*10-1
• Complex numbers
• Complex numbers are written as <real part> + <imaginary part>j
• i.e. 3+5j
• i.e. 15j
• Type: complex
• Strings
• Another important data type besides numbers are strings.
• Wrapped with the single-quote ( ' ) character:
'This is a string with single quotes'
• Wrapped with the double-quote ( " ) character:
“This is a string with double quotes"
• Wrapped with three characters, using either single-quote or double-quote:
'''A String in triple quotes can extend over multiple lines like this one, and
can contain 'single' and "double" quotes.'''
• Type : str
• There exists no character type in Python. A character is simply a string
of size one.
• Use the type() function to know which class a variable or a value
belongs
• Boolean
• Type: bool for Boolean values
• i.e. True (represented as 1 internally)
• i.e. False (represented as 0 internally)
Comments
Indenting Code
• The programmer can print the output of a value by using the print
function. The simplest form for using this function looks like the
following:
• print(argument)
• The argument can be a value of any type int, str, float etc. It can also be a
value stored in a variable.
• The input function is a simple way for your program to get information
from people using your program.
• The basic structure is
• Variable_name = input(message to user)
• Or
• Variable_name = input(‘string ’)
• The above works for getting text from the user. i.e. input( ) function
produces only string. A programmer can make use of any type to convert
the string into a specific type.
• i.e. x= int(input( ))
• i.e. y=float(input( ))
#Python Program
print('Enter your details :')
name=input("Enter your name:")
print(" Your Name is",name)
print(type(name))
age=input("Enter your age:")
print("Your age is",age)
print(type(age))
per=input("Enter your percentage:")
print("Your percentage is",per)
print(type(per))
#End of Program
#Python Program
print('Enter your details :')
name=input("Enter your name:")
print(" Your Name is",name)
print(type(name))
age=int(input("Enter your age:"))
print("Your age is",age)
print(type(age))
per=float(input("Enter your percentage:"))
print("Your percentage is",per)
print(type(per))
#Python Program
print('Enter your details :')
name=input("Enter your name:")
print(" Your Name is“,name)
print(type(name))
age=eval(input("Enter your age:"))
print("Your age is",age)
print(type(age))
per=eval(input("Enter your percentage:"))
print("Your percentage is",per)
print(type(per))
#End of Program
Use of eval( ) function avoids specifying a particular type (i.e. int,float )in
front of input( ) function.
• 57.464657 (10.2f)
5 7 . 4 6
• 12345678.93815 (10.3f)
1 2 3 4 5 6 7 8 . 9 3 8
• 57.4 (8.2f)
5 7 . 4 0
• 57 (<5.1f)
• By default, the number is right justified. You can insert < in the format
specifier to specify an item to be left justified.
5 7 . 1
• 625.421 (>4.1f)
• By default, the number is right justified. You can specify > in the format
to specify right justification.
6 2 5 . 4
1 2 7 8 5 6 . 4 8
• Formatting as a Percentage
• You can use the conversion code % to format a number as a percentage.
• The format causes the number to be multiplied by 100 and displayed with
a % sign following it. The total width includes the % sign counted as one
space.
• a) 0.0033923, "10.2%”
0.34% 0 . 3 4 %
• b) 0.0033923, "6.3%”
0.339% 0 . 3 3 9 %
• c) 7.4, "4.2%”
740.00% 7 4 0 . 0 0 %
• Formatting as a Percentage
• 57, "8.1%”
• 5700.0% 5 7 0 0 . 0 %
• Formatting Integers
• The conversion codes d, x, o, and b can be used to format an integer in
decimal, hexadecimal, octal, or binary. You can specify a width for the
conversion. DIVISION RESULT REMAINDER
• Decimal to Binary Conversion 256/2 128 0
• e.g. Convert 256 to binary 128/2 64 0
64/2 32 0
32/2 16 0
16/2 8 0
8/2 4 0
4/2 2 0
2/2 1 0
1/2 0 1
Answer 100000000
• 100,"10b”
• 1100100 1 1 0 0 1 0 0
• 256,"6o”
• 400 4 0 0
• Formatting Integers
• For example, Decimal to Hexadecimal conversion
• Conversions
• Decimal to Binary, octal and Hexadecimal conversions
• Formatting Strings
• You can use the conversion code s to format a string with a specified
width.
• By default, a string is left justified.
• To right-justify it, put the symbol > in the format specifier.
• If the string is longer than the specified width, the width is automatically
increased to fit the string.
• e.g.
• Format the string :
• a) Welcome to Python, 20s
W e l c o m e t o P y t h o n
J a s p r e e t S i n g h
• d) Python and Java support portability, >25s
P y t h o n a n d J a v a S u p p o r t P o r t a b i l i t y
J a s p r e e t S i n g h
Formatting Strings
THANKS…..