Python Fundamentals
Python Fundamentals
FUNDAMENTALS
PYTHON 3.6.5
PYTHON CHARACTER SET
Category Example
Letters A-Z, a-z
Digits 0-9
Special Space + - * / ** \ () [] {} // = != == < > . ‘ “ ‘’’
Symbols “”” , ; : % ! & # <= >= @ >>> << >>
_(underscore)
White Blank space, tabs, carriage return, new line,
Spaces form-feed
Other All other ASCII and Unicode characters
Characters
TOKEN
• Keyword
• Identifiers (Names)
• Literals
• Operators
• Punctuators
KEYWORDS
• A keyword is a word having special meaning reserved by the programming
language.
Example:
• x = (1 == True)
• y = (1 == False)
• a = True + 4
• b = False + 10
• print("x is", x)
• print("y is", y)
• print("a:", a)
• print("b:", b)
SPECIAL LITERAL
Python contains one special literal i.e. None
Example:
drink = "Available"
food = None
def menu(x):
if x == drink:
print(drink)
else:
print(food)
menu(drink)
menu(food)
LITERAL COLLECTIONS
• fruits = ["apple", "mango", "orange"] #list
• numbers = (1, 2, 3) #tuple
• alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
• vowels = {'a', 'e', 'i' , 'o', 'u'} #set
• print(fruits)
• print(numbers)
• print(alphabets)
• print(vowels)
OPERATORS
• Unary Operator (+, -, ~, not)
• Binary Operator
– Arithmetic Operator (+, -, *, /, %, **, //)
– Relational Operator (<, >, <=, >=, ==, !=)
– Logical Operator (and, or, not)
– Bitwise Operator (&, ^, |, <<, >>)
– Assignment Operator (=, +=, -=, *=, /=, %=,**=, //=)
– Identity Operator (is, not is)
– Membership Operator (in, not in)
PUNCTUATORS
a = 1; b = 2; c = 3
PYTHON INDENTATION
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
x = y = z = "same"
print (x)
print (y)
print (z)
PYTHON INPUT AND OUTPUT
• Python 3.x version provides print( ) function for output
• Printing a string
print(“Hello World”)
• Printing variable
a=10
print(a)
• Printing multiple variables
a=10
b=20
c=30
print(“Values of a, b and c =“, a, b, c)
PYTHON INPUT AND OUTPUT
• Printing formatted string
age=10
print(“Your age is {} years”.format(age))
• Printing formatted string (old style)
a=10
b=13.25
c=“Gwalior”
print(“a=%d, b=%f and c =%s“, a, b, c)
PYTHON INPUT AND OUTPUT
• Printing in a single line with multiple print()
a=25
b=15
c=20
print(a, end=‘ ’)
print(b, end=‘ ‘)
print(c, end=‘ ‘)
print( ) #for line change
print(a, b, c, sep=“:”)
PYTHON INPUT AND OUTPUT
• Taking input from keyboard at runtime
age=input(“Enter you age:”)
print(“Your age =“, age)
• eval function
e=eval(input(“Enter an expression :”)
print(“Result =“, e)