0% found this document useful (0 votes)
148 views31 pages

Python Fundamentals

This document provides an overview of Python fundamentals including the Python character set, tokens, keywords, identifiers, literals, operators, punctuators, statements, comments, variables, input/output, and modules. It describes the basic building blocks of the Python language such as letters, digits, whitespace, and special characters that make up the character set. It also defines tokens, keywords, identifiers, and rules for writing identifiers. Additionally, it covers various types of literals including strings, numbers, Booleans, special literals, and literal collections.

Uploaded by

Lap top
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
148 views31 pages

Python Fundamentals

This document provides an overview of Python fundamentals including the Python character set, tokens, keywords, identifiers, literals, operators, punctuators, statements, comments, variables, input/output, and modules. It describes the basic building blocks of the Python language such as letters, digits, whitespace, and special characters that make up the character set. It also defines tokens, keywords, identifiers, and rules for writing identifiers. Additionally, it covers various types of literals including strings, numbers, Booleans, special literals, and literal collections.

Uploaded by

Lap top
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

PYTHON

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.

Keywords in Python 3.3 programming language

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
MORE ON KEYWORDS….

• We cannot use a keyword as variable


name, function name or any other identifier.
• In Python, keywords are case sensitive.
• All the keywords except True, False and None are
in lowercase and they must be written as it is.
PYTHON 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.
• We cannot use special symbols like !, @, #, $, % etc. in our
identifier and space is also not allowed.
• Identifier can be of any length.
• Identifiers are case sensitive.
LITERALS
• String Literals • Boolean Literals
– Single Line String • Special Literals
– Multi Line String • Literal Collection
– Raw String – List
– Unicode String – Tuple
• Numeric Literals – Dictionary
– Integer – Set
– Float
– Complex
STRINGS
Single Line “hello world”
String
Multi Line “””Allahabad
String Uttar Pradesh”””
Raw String r"raw \n string"
Character "C"
Unicode u"\u00dcnic\u00f6de“,
string u"\u0930\u093E\u091C\u0940\u0935"
EXAMPLE
• strings = "This is Python"
• char = "C"
• multiline_str = """This is a multiline string with more than one
line code."""
• unicode = u"\u00dcnic\u00f6de"
• raw_str = r"raw \n string"
• print(strings)
• print(char)
• print(multiline_str)
• print(unicode)
• print(raw_str)
NUMERIC LITERALS
• Integer
– Decimal
– Binary Literal
– Octal
– Hexadecimal
• Float
• Complex
NUMERIC LITERALS
Decimal 12, -25, 10035
Binary 0b1010
Octal 0o12, 0O35
Hexadecimal 0x31A2, 0X4A
Float 3.5, 2000.152, 135.75e25, -3.2e3
Complex Number (2 + 3.5j)
NUMERIC LITERAL
EXAMPLES
• a = 0b1010 #Binary Literals
• b = 100 #Decimal Literal
• c = 0o310 #Octal Literal
• d = 0x12c #Hexadecimal Literal
• #Float Literal
• float_1 = 10.5
• float_2 = 1.5e2
• #Complex Literal
• x = 3.14j
• print(a, b, c, d)
• print(float_1, float_2)
• print(x, x.imag, x.real)
BOOLEAN LITERALS
• There are two kinds of Boolean literal: True and False.

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

• Most common punctuators in python are-


' " # \ ( ) [ ] { } @ ,:.= ;
PYTHON STATEMENT
• Instructions that a Python interpreter can execute are called
statements.
• For example, a = 2 is an assignment statement
• if statement, for statement, while statement etc. are other
kinds of statements.
MULTI-LINE STATEMENT
• In Python, end of a statement is marked by a newline
character. But we can make a statement extend over multiple
lines with the line continuation character (\). For example:
a=1+2+3+\
4+5+6+\
7+8+9
MULTI-LINE STATEMENT
• This is explicit line continuation. In Python, line continuation
is implied inside parentheses ( ), brackets [ ] and braces { }.
For instance, we can implement the above multi-line
statement as
a = (1 + 2 + 3 +
4+5+6+
7 + 8 + 9)
MULTIPLE STATEMENT IN
SINGLE LINE
• We could also put multiple statements in a single line
using semicolons, as follows

a = 1; b = 2; c = 3
PYTHON INDENTATION

• Most of the programming languages like C, C++, Java use


braces { } to define a block of code. Python uses
indentation.
• A code block (body of a function, loop, class etc.) starts
with indentation and ends with the first unindented line.
• The amount of indentation is up to you, but it must be
consistent throughout that block.
• Generally four whitespaces are used for indentation and is
preferred over tabs.
PYTHON COMMENTS
Single Line Comment:
• In Python, we use the hash (#) symbol to start writing a comment.
#This is a comment
#print out Hello
print('Hello')
Multi-line comments
• Another way of doing this is to use triple quotes, either ''' or """.
"""This is also a
perfect example of
multi-line comments""“
print(“Hello”)
DOCSTRING IN PYTHON
• Docstring is short for documentation string.
• It is a string that occurs as the first statement in a module, function,
class, or method definition.
• Triple quotes are used while writing docstrings. For example:
def double(num):
"""Function to double the value"""
return 2*num
• Docstring is available to us as the attribute __doc__ of the function.
Issue the following code in shell once you run the above program.
>>> print(double.__doc__)
Function to double the value
DECLARING VARIABLES IN
PYTHON
Assigning multiple values to multiple variables

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)

• Taking input from keyboard at runtime(old style)


e=raw_input(“Enter an expression”)
print(“Expression is “, e)
PYTHON MODULES

You might also like