Unit 1 Introduction To Python
Unit 1 Introduction To Python
40
the above code works. this means Python is high level programming.
2) void main()
3) {
4) printf("Hello World");
5) }
in Java
1) public class HelloWorld
2) {
4) {
5) System.out.println("Hello world");
6) }
7) }
In Python
In [3]: print("Hello World")
Hello World
for example
In [6]: print('Python is fun!!')
print(5+3)
print(f+3)
Python is fun!!
8
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 3
1 print('Python is fun!!')
2 print(5+3)
----> 3 print(f+3)
In above example first two prints executed properly while in 3rd print
command error occured. this means python read and execute code line
by line
Eg: a = 20
a=50
a_sum=20
a$hey=30 # $ is not allowed
Cell In[2], line 5
a$hey=30 # $ is not allowed
^
SyntaxError: invalid syntax
total=10
TOTAL=999
print(total) #10
print(TOTAL) #999
10
999
Keywords in python
In Python some words are reserved to represent some meaning or
functionality. Such type of words are called
Reserved words.
following code ask python to list all its reserved words (Keywords)
In [6]: import keyword
print(keyword.kwlist)
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'br
eak', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'fr
om', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'r
aise', 'return', 'try', 'while', 'with', 'yield']
In [2]: # keywords should not be used for identifier they are reserved by python
for=5
print(for)
num=15
amount=256.87
code='A'
pi=3.1415926536
population_of_India=1400000000
your_message='hey!'
print("num= "+str(num))
print("Amount= "+str(amount))
print("CODE= "+code)
print("\nPopulation of India is : "+str(population_of_India))
print("\nMessage: "+your_message)
num= 15
Amount= 256.87
CODE= A
Message: hey!
0
waw
In [5]: # In, Python every input value is treated as str type only.
print(type(x))
print(type(y))
print("ABCD")
ABCD
A B C D
A $ B $ C $ D
G#F#G+Python is fun
10:20:30***40:50:60
70**80$$90 100
name="upendra"
password=1234
sport="kabaddi"
print(f"hey {name}!, your favourite sports is {sport} and your passowrd is {password}")
hey upendra!, your favourite sports is kabaddi and your passowrd is 1234
hey upendra!, your favourite sports is 1234 and your passowrd is kabaddi
In [15]: # you can use both method in same program and everywhere
name="upendra"
password=1234
sport="kabaddi"
print(f"hey {name}!, your favourite sports is {sport} and your passowrd is {password}")
print("hey {}!, your favourite sports is {} and your passowrd is {}".format(name,sport,p
hey upendra!, your favourite sports is kabaddi and your passowrd is 1234
hey upendra!, your favourite sports is kabaddi and your passowrd is 1234
** TypeCasting **
Input() takes values in string data type.
Which is not always desirable and sometimes we may need to convert it.
Mainly type casting can be done with these data type functions:
1. Int(): Python Int() function take float or string as an argument and returns int type object.
2. float(): Python float() function take int or string as an argument and return float type object.
3. str(): Python str() function takes float or int as an argument and returns string type object.
current_year=2024
year_of_birth=input("Please enter your year of Birth: ") # Python will consider this s
age=current_year-int(year_of_birth)
print(a,b,c)
S=(a+b+c)/2
area=((S*(S-a)*(S-b)*(S-c))**0.5)
print("Area is "+str(area))
In [22]: # Write a python program to print the unit digit of any number
# hint: if you divide given number by 10, the reminder will be the unit digit
avg=(num_1+num_2)/2
dev_1=num_1-avg
dev_2=num_2-avg
# Calcualtion
simple_interest = (principal*time*rate)/100
compound_interest = principal * ( (1+rate/100)**time - 1)
# Displaying result
print('Simple interest is: %f' % (simple_interest))
print('Compound interest is: %f' %(compound_interest))
In [ ]:
Python Libraries
A Python library is a collection of related modules. It contains bundles of
code that can be used repeatedly in different programs. It makes Python
Programming simpler and convenient for the programmer. As we don’t
need to write the same code again and again for different programs.
Python libraries play a very vital role in fields of Machine Learning, Data
Science, Data Visualization, etc.
print(sqrt(24))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 3
1 # Suppose we want to take square root of any given number
----> 3 print(sqrt(24))
print(np.sqrt(24))
4.898979485566356
6 1 1 5 5 7
In [ ]: