0% found this document useful (0 votes)
31 views8 pages

Introduction to Python Programming

Hldkbdodbkldbdilevdldbpdmduxodlnebdpmdndimd

Uploaded by

trlohith60
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

Introduction to Python Programming

Hldkbdodbkldbdilevdldbpdmduxodlnebdpmdndimd

Uploaded by

trlohith60
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

#Introduction to Python

In [ ]:

1 #Python is a popular programming language.


2 #It was created by Guido van Rossum, and released in 1991
3 #It is used for:
4 #web development (server-side),
5 #software development,
6 #mathematics,
7 #system scripting.
8
9 #What can Python do?
10 #Python can be used on a server to create web applications.
11 #Python can be used alongside software to create workflows.
12 #Python can connect to database systems. It can also read and modify files.
13 #Python can be used to handle big data and perform complex mathematics.
14 #Python can be used for rapid prototyping, or for production-ready software development.
15 #Why Python?
16 #Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
17 #Python has a simple syntax similar to the English language.
18 #Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
19 #Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping
20 #Python can be treated in a procedural way, an object-oriented way or a functional way.
21

In [1]:

1 #Printing
2 x = 5
3 y = "Hello, World!"
4
5 print(x)
6 print(y)

5
Hello, World!

In [ ]:

1 #This is a comment.
2 print("Hello, World!")

Hello, World!

In [ ]:

1 print("Hello, World!") #This is a comment.

Hello, World!

In [ ]:

1 #Comments can be used to explain Python code.


2 #Comments can be used to make the code more readable.
3 #Comments can be used to prevent execution when testing code.
4 print("Hello, World!")

Hello, World!

In [ ]:

1 """
2 This is a comment
3 written in
4 more than just one line
5 """
6 print("Hello, World!")

Hello, World!

In [ ]:

1 #Creating Variables
2 #Python has no command for declaring a variable.
3 #A variable is created the moment you first assign a value to it.
4 x =5
5 y = "John"
6 print(x)
7 print(y)

5
John

localhost:8888/notebooks/Python_Introduction_l1.ipynb 1/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 #Variables do not need to be declared with any particular type, and


2 #can even change type after they have been set.
3 x = 4 # x is of type int
4 print(x)
5 x = "Sally" # x is now of type str
6 print(x)

4
Sally

In [ ]:

1 #Casting
2 #If you want to specify the data type of a variable, this can be done with casting.
3 x = str(3) # x will be '3'
4 y = int(3) # y will be 3
5 z = float(3) # z will be 3.0
6 print(x)
7 print(y)
8 print(z)

3
3
3.0

In [ ]:

1 rno= input()

#Python Data Types

In [ ]:

1 #Built-in Data Types


2 # In programming, data type is an important concept.
3 # Variables can store data of different types, and different types can do different things.
4 # Python has the following data types built-in by default, in these categories:
5
6 #Text Type: str
7 #Numeric Types: int, float, complex
8 #Sequence Types: list, tuple, range
9 #Mapping Type: dict
10 #Set Types: set, frozenset
11 #Boolean Type: bool
12 #Binary Types: bytes, bytearray, memoryview

In [ ]:

1 #You can get the data type of a variable with the type() function.
2 x = 5
3 y = "John"
4 print(type(x))
5 print(type(y))

<class 'int'>
<class 'str'>

In [ ]:

1 #Python Numbers
2 x = 1 # int
3 y = 2.8 # float
4 z = 1j # complex
5 print(type(x))
6 print(type(y))
7 print(type(z))
8

<class 'int'>
<class 'float'>
<class 'complex'>

localhost:8888/notebooks/Python_Introduction_l1.ipynb 2/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 x = 1
2 y = 35656222554887711
3 z = -3255522
4
5 print(type(x))
6 print(type(y))
7 print(type(z))

<class 'int'>
<class 'int'>
<class 'int'>

In [ ]:

1 x = 1.10
2 y = 1.0
3 z = -35.59
4
5 print(type(x))
6 print(type(y))
7 print(type(z))

<class 'float'>
<class 'float'>
<class 'float'>

In [ ]:

1 x = 3+5j
2 y = 5j
3 z = -5j
4
5 print(type(x))
6 print(type(y))
7 print(type(z))

<class 'complex'>
<class 'complex'>
<class 'complex'>

In [ ]:

1 #Type Conversion
2 x = 1 # int
3 y = 2.8 # float
4 z = 1j # complex
5
6 #convert from int to float:
7 a = float(x)
8
9 #convert from float to int:
10 b = int(y)
11
12 #convert from int to complex:
13 c = complex(x)
14
15 print(a)
16 print(b)
17 print(c)
18
19 print(type(a))
20 print(type(b))
21 print(type(c))

1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

In [ ]:

1 #Random number generation


2 import random
3 print([Link](1, 10))

localhost:8888/notebooks/Python_Introduction_l1.ipynb 3/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 #Python Casting
2 x = float(1)
3 y = float(2.8)
4 z = float("3")
5 w = float("4.2")
6 print(x)
7 print(y)
8 print(z)
9 print(w)

1.0
2.8
3.0
4.2

In [ ]:

1 #Converting to string
2 x = str("s1")
3 y = str(2)
4 z = str(3.0)
5 print(x)
6 print(y)
7 print(z)

In [ ]:

1 #Python Strings
2 #Strings in python are surrounded by either single quotation marks, or double quotation marks.
3 #You can use double or single quotes:
4
5 print("Hello")
6 print('Hello')
7

Hello
Hello

In [ ]:

1 a = """Hello Dear Participants


2 Welcome to the FDP
3 Enjoy the DL Sessions"""
4 print(a)

Hello Dear Participants


Welcome to the FDP
Enjoy the DL Sessions

In [ ]:

1 #Strings are Arrays


2 a = "Hello, World!"
3 print(a[1])

In [ ]:

1 #Looping Through a String


2 for x in "banana":
3 print(x)

b
a
n
a
n
a

In [ ]:

1 #String length1
2 a = "Hello, World!"
3 print(len(a))

13

#Conditional Statements

localhost:8888/notebooks/Python_Introduction_l1.ipynb 4/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 a = 33
2 b = 200
3 if b > a:
4 print("b is greater than a")

b is greater than a

In [ ]:

1 #Indentation
2 a = 33
3 b = 200
4
5 if b > a:
6 print("b is greater than a")

In [ ]:

1 #Elif
2 a = 33
3 b = 33
4 if b > a:
5 print("b is greater than a")
6 elif a == b:
7 print("a and b are equal")

a and b are equal

In [ ]:

1 #else
2 a = 200
3 b = 33
4 if b > a:
5 print("b is greater than a")
6 elif a == b:
7 print("a and b are equal")
8 else:
9 print("a is greater than b")

a is greater than b

In [ ]:

1 a = 200
2 b = 33
3 if b > a:
4 print("b is greater than a")
5 else:
6 print("b is not greater than a")

b is not greater than a

In [ ]:

1 #Short Hand If
2 a = 200
3 b = 33
4
5 if a > b: print("a is greater than b")

a is greater than b

In [ ]:

1 #Short Hand If ... Else


2 a = 2000
3 b = 330
4
5 print("A") if a > b else print("B")

In [ ]:

1 a = 330
2 b = 330
3
4 print("A") if a > b else print("=") if a == b else print("B")

localhost:8888/notebooks/Python_Introduction_l1.ipynb 5/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 #and
2 a = 200
3 b = 33
4 c = 500
5 if a > b and c > a:
6 print("Both conditions are True")

Both conditions are True

In [ ]:

1 #or
2 a = 200
3 b = 33
4 c = 500
5 if a > b or a > c:
6 print("At least one of the conditions is True")

At least one of the conditions is True

In [ ]:

1 #nested if
2 x = 41
3
4 if x > 10:
5 print("Above ten,")
6 if x > 20:
7 print("and also above 20!")
8 else:
9 print("but not above 20.")

Above ten,
and also above 20!

In [ ]:

1 a = 33
2 b = 200
3
4 if b > a:
5 pass
6
7
8 # having an empty if statement like this, would raise an error without the pass statement

#Python Loops

In [ ]:

1 #Python has two primitive loop commands:


2 #while loops
3 #for loops
4 i = 1
5 while i < 6:
6 print(i)
7 i += 1
8

1
2
3
4
5

In [ ]:

1 i = 1
2 while i < 6:
3 print(i)
4 if (i == 3):
5 break
6 i += 1

1
2
3

localhost:8888/notebooks/Python_Introduction_l1.ipynb 6/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 i = 0
2 while i <= 6:
3 i += 1
4 if i == 3:
5 continue
6 print(i)
7
8 # Note that number 3 is missing in the result

1
2
4
5
6
7

In [ ]:

1 #else
2 i = 1
3 while i < 6:
4 print(i)
5 i += 1
6 else:
7 print("i is no longer less than 6")

1
2
3
4
5
i is no longer less than 6

In [ ]:

1 fruits = ["apple", "banana", "cherry"]


2 for x in fruits:
3 print(x)

apple
banana
cherry

In [ ]:

1 for x in range(6):
2 print(x)

0
1
2
3
4
5

In [ ]:

1 for x in range(2, 6):


2 print(x)

2
3
4
5

In [ ]:

1 for x in range(2, 30, 3):


2 print(x)

2
5
8
11
14
17
20
23
26
29

localhost:8888/notebooks/Python_Introduction_l1.ipynb 7/8
30/01/2023, 20:03 Python_Introduction_l1 - Jupyter Notebook

In [ ]:

1 for x in range(6):
2 print(x)
3 else:
4 print("Finally finished!")

0
1
2
3
4
5
Finally finished!

In [ ]:

1 adj = ["red", "big", "tasty"]


2 fruits = ["apple", "banana", "cherry"]
3
4 for x in adj:
5 for y in fruits:
6 print(x, y)

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

localhost:8888/notebooks/Python_Introduction_l1.ipynb 8/8

You might also like