Professional Documents
Culture Documents
Python Programming Ch1 Unit1
Python Programming Ch1 Unit1
4. Identifiers are case sensitive. Of course Python language itself is case sensitive
language.
Ex: total=10
TOTAL=999
print(total) o/p : 10
print(TOTAL) o/p : 999
4.Keywords cannot be used as identifiers.
Ex: x = 10 # Valid
if = 33 # In valid if is a keyword in Python
SyntaxError: invalid syntax
5. Identifiers can be of any length.
Ex : Abcedwrtyyhfdg123_=10
Q. Which of the following are valid Python identifiers?
123total = 22
total1234 = 22
java2share = 'Java‘
ca$h = 33
_abc_abc_ = 22
def = 44
for = 3
_p = 33
Keywords:
1. In Python some words are reserved to represent some meaning or functionality.
Such type of words are called Reserved words or keywords.
2. There are 33 reserved words available in Python.
List of keywords in python:
and as not
assertfinally or
break for pass
class from nonlocal
continue global raise
defif return
del import try
elif in while
else is with
except lambda yield
False True None
2. All 33 keywords contains only alphabets symbols.
3. Except the following 3 reserved words, all contain only lowercase alphabet
symbols.
True
False
None
4. True and False are Boolean values , For Boolean values, compulsory you
need to use capital letter 'T' in True and capital letter 'F' in False.
Ex: a = True # Valid
A=true # In valid
NameError: name 'true' is not defined
Statements and Expressions:
A statement is an instruction or statement is a unit of code that can be executed by
python interpreter.
Eg: z = 1 // this iExamples: Y=x + 17 >>> x=10 >>> z=x+20 >>> z 30 s an assignment
statement
Expression:
An expression is a combination of values, variables, and operators which are evaluated
to make a new value b.
>>> 20 # A single value
>>> z # A single variable
>>> z=10 # A statement
But expression is a combination of variable, operator and value which is evaluated by
using assignment operator in script mode.
Examples: Y=x + 17
>>> x=10 >>> z=x+20 >>> z o/p : 30
When the expression is used in interactive mode, is evaluated by the interpreter
and the result is displayed instantly.
Eg:
>>> 8+2
10
Variables:
Variables are nothing but reserved memory locations to store
values. That means when you create a variable some space is
reserved in memory.
• One of the most powerful features of a programming language is
the ability to manipulate variables.
• A variable is a name that refers to a value. An assignment statement
creates new variables and gives them values:
•Data types specify the type of data like numbers and characters to be stored and
manipulated with in a program. Basic data type of python are
•Numbers
•Boolean
•Strings
•None
Numbers:
•Integers, floating point numbers and complex numbers fall under python numbers
category. They are defined as int, float and complex class in python.
1. integer:
•Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length, it is only limited by the memory available.
Example:
a=10
b=-12
c=123456789
2. float:
• Float or "floating point number" is a number, positive or negative, containing one or more
decimals.
Example:
• X=1.0
• Y=12.3
• Z= -13.4
3. complex:
• Complex numbers are written in the form , “x+yj" where x is the real part and y is the
imaginary part.
Example:
A=2+5j
B=-3+4j
C=-6j
Boolean:
Booleans are essential when you start using conditional statements.
Python Boolean type is one of the built-in data types provided by Python, which
represents one of the two values i.e. True or False. The boolean values, True and False
treated as reserved words.
String:
• The string can be defined as the sequence of characters represented in the
quotation marks. In python, we can use single, double, or triple quotes to
define a string.
• In the case of string handling, the operator + is used to concatenate two
strings as the operation "hello"+" python" returns "hello python".
Example:
EX : S1=‘Welcome’ #using single quotes
S1 Output: ‘Welcome’
print(S1) Output: Welcome
Ex: S2=“To” #using double quotes
S2 Output: 'to'
print(S2) Output: to
Ex: S3=‘’’Python’’’ #using triple quotes
S3 Output: "'python'"
print(S3) Output: 'python‘
Ex: Name1= ‘Hello’
Name2=‘python’
Name1 + Name2 Output: ‘Hellopython’
print(Name1 + Name2) Output: Hellopython
Example:
a=10
b=“Python"
c = 10.5
d=2.14j
e=True
print("Data type of Variable a :",type(a))
print("Data type of Variable b :",type(b))
print("Data type of Variable c :",type(c))
print("Data type of Variable d :",type(d))
print(“Data type of Variable e :”,type(e))
Output:
• Data type of Variable a : <class 'int'>
• Data type of Variable b : <class 'str'>
• Data type of Variable c : <class 'float'>
• Data type of Variable d : <class 'complex'>
• Data type of Variable e : <class 'bool'>
Indentation:
• In Python it is a requirement and not a matter of style to indent the
program. This makes the code cleaner and easier to understand and
read.
• If a code block has to be deeply nested, then the nested statements
need to be indented further to the right.
• Block 2 and Block 3 are nested under Block 1. 4 white spaces are
used for indentation and are preferred over tabs.
• Incorrect indentation will result in IndentationError.
Block 1
Block 2
Block 3
Block 2, Continuation
Block 1, Continuation
• Reading Input :
• Input() function is used to gather data from the user.
• Syntax : variable_name = input([prompt])
• Where prompt is a string written inside parenthesis.
• The prompt gives an indication to the user of the value that needs to
be entered through the keyboard.
• When user presses Enter key, the program resumes and input
function returns what the user typed as a string.
• Even if the user inputs a number, it is treated as a string and the
variable_name is assigned the value of string.
1. >>> person = input(“what is your name?”)
2. What is your name? John
3. >>>person
4. ‘John’
Print Output
• Print() function allows a program to display text onto the console.
• Print function prints everything as strings.
>>>print(“Hello World”)
Hello World
There are two major string formats which are used inside the print() function to
display the contents on to the console.
1. str.format()
2. f-strings
str.format() method:
• The format() method returns a new string with inserted values.
• syntax: str.format(p0,p1,p2…..k0=v0, k1=v1,…)
• Where p0,p1 are called as positional arguments and k0,k1 are keyword arguments
with their assigned values of v0,v1..
• Positional arguments are a list of arguments that can be accessed with an index of
argument inside curly braces like {index}. Index value starts from zero.
• Keyword arguments are a list of arguments of type keyword = value, that can be
accessed with the name of the argument inside curly braces like {keyword}.
Eg: country = input(“which country do you live in”)
print(“I live in {0}”.format(country))
• Print Output
Eg:
a = 10
b = 20
Print(“the values of a is {0} and b is {1}”.format(a,b))
Print(“the values of b is {1} and a is {0}”.format(a,b))
f-strings:
A f-string is a string literal that is prefixed with “f”. These strings
may contain replacement fields ,and are enclosed within
curly braces { }.
Eg: country = input(“which country do you live in”)
Print(f”I live in {country}”)
Type Conversion in Python:
• Python provides Explicit type conversion functions to directly convert
one data type to another. It is also called as Type Casting in Python
• Python supports following functions
1. int () : This function convert a float number or a string to an integer.
• Eg: float_to_int = int(3.5)
string_to_int = int(“1”) // number is treated as string
print(float_to_int) o/p: 3
print(string_to_int)o/p: 1 will be displayed.
2. float( ) :
• This function convert integer or a string to floating point number using the float()
function.
• Eg: int_to_float = float(8)
string_to_float = float(“1”) // number is treated as string
print(int_to_float)
print(string_to_float)
8.0
1.0 get displayed
• The str() Function : The str() function returns a string which is fairly
human readable.
Eg: int_to_string =str(8)
float_to_string = str(3.5)
Print(int_to_string) prints ‘8’
Print(float_to_string) prints ‘3.5’