Review of Python Basics
Review of Python Basics
Review of Python Basics
BASICS
.
2. 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.
3. Keywords cannot be used as identifiers.
>>> global = 1
File "<interactive input>", line 1
global = 1
^
SyntaxError: invalid syntax
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
>>> a@ = 0
File "<interactive input>", line 1
a@ = 0
^
SyntaxError: invalid syntax
5. Identifier can be of any length
• NOTE: python is a case sensitive lang. so variable and Variable are two different things.
Built-in Data Types
• This type conversion is also called typecasting because the user casts
(change) the data type of the objects
• Syntax :
(required_datatype)(expression)
Addition of string and integer using explicit conversion:
num_int = 123
num_str = "456“
print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type casting:",type(num_str))
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
OUTPUT:
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>
Data type of num_str after Type Casting: <class 'int'>
Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>
Dynamic typing
• Why python is a dynamically typed language.
Python is a dynamically typed language. It doesn’t know about the type
of the variable until the code is run. So declaration is of no use. What it
does is, It stores that value at some memory location and then binds
that variable name to that memory container. And makes the contents
of the container accessible through that variable name. So the data type
does not matter. As it will get to know the type of the value at run-time.
Creating a String
• Strings in Python can be created using single quotes or double quotes
or even triple quotes
• Built in functions related to string :
text = "program is fun“
print(text.zfill(15))
print(text.zfill(20))
print(text.zfill(10))
O/P
0program is fun
000000program is fun
program is fun
Method Description
Python String endswith() Checks if String Ends with the Specified Suffix
Python String startswith() Checks if String Starts with the Specified String
Python String zfill() Returns a Copy of The String Padded With Zeros
List = [1,2,3]
li=list([23,45]) Method Description
def myfun(*name):
print('Hello', name)
myfun('my', 'school')
O/P:
Hello ('my', 'school')
FIND O/P:
x = 150
def myfunc():
global x
print('x is', x)
x = x*2
print('Changed global x to', x)
myfunc()
print('Value of x is', x)
x is 150
Changed global x to 300
Value of x is 300
O/P:
def a(n):
if n==0:
return 0
elif n==1:
return 1
else:
return a(n-1)+a(n-2)
for i in range(0,6):
print(a(i),end=" ")
011235