0% found this document useful (0 votes)
10 views20 pages

Python Basics: Syntax and Data Types

The document outlines a Python programming session covering topics such as syntax, data types, variables, comments, user input, and type conversion. It includes examples of printing various data types, defining variables, and using keywords and identifiers. Additionally, it provides tasks for practical application of the concepts learned, including user input and calculations.

Uploaded by

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

Python Basics: Syntax and Data Types

The document outlines a Python programming session covering topics such as syntax, data types, variables, comments, user input, and type conversion. It includes examples of printing various data types, defining variables, and using keywords and identifiers. Additionally, it provides tasks for practical application of the concepts learned, including user input and calculations.

Uploaded by

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

Session-1

Today’s Topics:
 Syntax
 Printing ways in python
 Comments
 Python Output
 Data Types
 Variables
 Static Typing
 Dynamic Typing
 Static Binding
 Dynamic Binding
 Keywords & Identifiers
 User Input
 Type Conversion
 Implicit
 Explicit
 Literals
 Integer
 Float
 Complex

What is Syntax?
Grammar of writing any code in programming language
is called as syntax

Example:
Solution=>
print(Teknowell EduTech)
Note: Above not allowed in python
Correct code

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Solution=>
print("Teknowell EduTech")

What is Comments?
Non-executable part of code is called as comment.

Types:
1 Single line(#): if you want to comment for
single line
2 Multiline: use below for multiline
("""this is a comment
second line"""
)

Note: Python is case sensitive language

Example: Printing string using single quotes


Solution=>
print('Teknowell EduTech')

Example: Printing string using double quotes


Solution=>
print("Teknowell EduTech")

Example: Printing string using triple single quotes


Solution=>

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print('''Teknowell EduTech''')

Example: Printing string using triple double quotes


Solution=>
print("""Teknowell EduTech""")

Example: Printing integer number


Solution=>
print(7)

Example: Printing float number


Solution=>
print(7.7)

Example: Printing boolean data


Solution=>
print(True)
print(False)

Example: Printing various datatypes in single print


statement
Solution=>
print("Hello", 1, 4.5, True)

Example: sep parameter of print function


Solution=>

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print("Hello", 1, 4.5, True, sep = " / ")

Example: Print function by default takes new line


Solution=>
print("Hello")
print("world")

Example: end parameter of print function


Solution=>
print("hello", end = " - ")
print("world")

Example: Integer (Whole numbers)


Solution=>
print(8)

Example: Print highest integer value


Solution=>
print(1e308)
Note: 1e308 is the largest possible integer number in
python

Example: When we get infinite value


Solution=>
print(1e309)

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Example: Float (Decimal numbers)
Solution=>
print(8.55)

Example: Print highest floating value


Solution=>
print(1.7e308)

Example: When we get infinite value


Solution=>
print(1.7e309)

Example: Boolean (True or False)


Solution=>
print(True)
print(False)

Example: Complex (Real part plus imaginary part)


Solution=>
print(5 + 6j)
print(3 - 2j)

List: Collection of same or different data type


values

Example: Create Homogenous list

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Solution=>
print([1, 2, 3, 4, 5])

Example: Create Heterogeneous list


Solution=>
print([1, 2.67, "Chinchwad", False])

Tuple: Collection of same or different datatype


values

Example: Create Homogenous tuple


Solution=>
print((1, 2, 3, 4, 5))

Example: Create Heterogenous tuple


Solution=>
print((1, 2.67, "Chinchwad", False))

Sets: Collection of unique same or different datatype


values

Example: Create Homogenous set


Solution=>
print({1, 2, 3, 4, 5})

Example: Takes only unique data

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Solution=>
print({1, 2, 3, 1, 2})

Example: Heterogenous set


Solution=>
print({1, 2.67, "Chinchwad", False, 2.67})

Dictionary: Collection of key and value pairs


Solution=>
print({"Name" : "Raj", "Gender" : "Male", "Weight" :
70})

Example: type function in python


Solution=>
print(type("2"))
print(type(2))
print(type(2.67))
print(type(2 - 5j))
print(type(True))
print(type([1, 2, 3, 4, 5]))
print(type((1, 2, 3, 4, 5)))
print(type({1, 2, 3, 4, 5}))
print(type({"Name" : "Raj", "Gender" : "Male",
"Weight" : 70}))

Variables: whose value changes continuously

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Types of Typing in Python
Static Typing: Need to declare datatype of variable
before assigning value to it
int a = 5
Dynamic Typing: No need to declare datatype of
variable before assigning value to it
a = 5
Note: Python is dynamically typed language

Types of Binding in Python


Static Binding: Once variable is declared it's value
can be updated in same datatype
int a = 5
a = 10
Note: Above is valid syntax

int a = 5
a = "Teknowell"
Note: Above is invalid

Dynamic Binding: Once variable is declared it's value


can be updated in any datatype
a = 5
print(a)
a = "Teknowell"
print(a)
Note: Python is dynamically binded language

Example:

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
a = 1
b = 2
c = 3
print(a, b, c)

Note: You can write same above code as given below


a, b, c = 1, 2, 3
print(a, b, c)

Note: if you want to store same value for all the


variables then use the given
a = b = c = 5
print(a, b, c)

Local & Global Variables in Python:


Example:
x = 10 # Global
def show():
x = 5 # Local
print(x)

show() # 5
print(x) # 10

You can increment/decrement global variable inside a


scope.

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Example:
x = 10 # Global
def show():
x = x+5 # Local
print(x)
show() # 5
print(x) # 10

Correct code:
x = 10 # Global
def show():
global x
x+=5 # Local
print(x)
show() # 5
print(x) # 10

Use of nonlocal:
Example:
def outer():
x = 5

def inner():
x = 10 # This creates a new local x, doesn't
change outer's x
print("Inner x:", x)

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
inner()
print("Outer x:", x)

outer()
o/p:
Inner x: 10
Outer x: 5

Example:
Using nonlocal
def outer():
x = 5

def inner():
nonlocal x
x = 10 # This modifies x in outer()
print("Inner x:", x)

inner()
print("Outer x:", x)

outer()

o/p:
Inner x: 10
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Outer x: 10

Example:
def counter():
count = 0

def increment():
nonlocal count
count += 1
return count

return increment

c = counter()
print(c()) # 1
print(c()) # 2
print(c()) # 3

nonlocal vs global
Modifies variable
Keyword Example use
from...
Access top-level/global
global Global scope
variable
nonloca Enclosing (non- Access outer function
l global) scope variable

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Keywords & Identifiers:-
Keywords: Reserved words in any programming language
are called as Keywords. In python there are total 35
keywords
int, str, True, False, if else, while, for .......
etc
Identifiers: Name assigned to any variable is called
as identifier

Rules to declare identifiers


 Identifier can't start with a digit
 Only use of underscore(-) is allowed in
identifier
 Identifier cannot be a keyword

Example: Find out all the keywords of python


Solution:
import keyword
print([Link])
print("Total keywords in python are",
len([Link]))

Example:
1name = "Teknowell" # invalid
name1 = "Teknowell" # valid
print(name1)

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Example:
$ = "EduTech" #invalid

_ = "EduTech" #valid
print(_)

and = 34 #invalid

User Input
input("Enter Email : ")

Q. Write a program to take two numbers from user and


print their addition
fnum = input("Enter first number : ")
snum = input("Enter second number : ")
print(type(fnum), type(snum))
result = fnum + snum
print(result)
print(type(fnum))

Q. Write a program that will **tell** the number of


goats and chicken there are when the user will
provide the value of total heads and legs.
For example:
Input:
heads -> 4
legs -> 12
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
<br>
Output:
goats -> 2
chicken -> 2

Answer:
heads=int(input("Enter No. of Heads:"))
legs=int(input("Enter No. of legs:"))
#c+d=4------------head
#2c+4g=12------------legs
# 2c=12-4g
# c=(12-4g)/2
#g=(12-2c)/4
chicken=(legs-4*4)/2
goats=(legs-2*2)/4
print(goats)
print(chicken)

Note: Default datatype of input function is string.

6. Type Conversion
Converting one datatype to another datatype
Implicit conversion: Python Interpreter/compiler
converts data types automatically

Example:

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
print(5 + 5.6)
#Python automatically converts integer to float
print(type(5), type(5.6))
print(type(5 + 5.6))

Example:
a = True + 4
b = False + 10
print("a :", a)
print("b :", b)
print(4 + "4")
#Python is not able to convert this statement

Explicit: Developer manually converts datatype


int to str
print(str(5))
print(type(str(5)))

int to float
print(float(4))
print(type(float(5)))

Literals
Integer Literal
a = 0b1010
# Binary Literals
Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
# a -> variable,
#= -> assignment operator
#0b1010 ->literal
b = 100 # Decimal Literal
c = 0o310 # Octal Literal
d = 0x12c # Hexadecimal Literal

Float Literal
float_1 = 10.5
float_2 = 1.5e2 # 1.5 * 10^2
float_3 = 1.5e-3 # 1.5 * 10^-3

Complex Literal
x = 3.14j
print(a, b, c, d)
print(float_1, float_2,float_3)
print(x, [Link], [Link])

Note: If you want to print the sentences what you


write in code as per your structure then use <br> for
new line or use a backtick(```) three time

Example:
One<br>
Two<br>
Three

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Or
```
One
Two
Three
```

Session-1-Task
Q1 :- Print the given strings as per stated format.

**Given strings**:

"Teknowell" "Edutech" "Chinchwad"

**Output**:

Teknowell-Edutech-Chinchwad

Concept- [Seperator and End]

Q2:- Write a program that will convert celsius value


to fahrenheit.

Q3:- Take 2 numbers as input from the [Link] a


program to swap the numbers without using any special
python syntax.

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Q4:- Write a program to find the euclidean distance
between two [Link] both the coordinates
from the user as input.

Q5:- Write a program to find the simple interest when


the value of principle,rate of interest and time
period is provided by the user.

Q6:- Write a program that will tell the number of


dogs and chicken are there when the user will provide
the value of total heads and legs.

For example:
Input:
heads -> 4
legs -> 12
<br>
Output:
dogs -> 2
chicken -> 2

Q7:- Write a program to find the sum of squares of


first n natural numbers where n will be provided by
the user.

Q8:- Given the first 2 terms of an Arithmetic


[Link] the Nth term of the series. Assume all
inputs are provided by the user.

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.
Q9:- Given 2 fractions, find the sum of those 2
[Link] the numerator and denominator values
of the fractions from the user.

Q10:- Given the height, width and breadth of a milk


tank, you have to find out how many glasses of milk
can be obtained? Assume all the inputs are provided
by the user.

Input:<br>
Dimensions of the milk tank<br>
H = 20cm, L = 20cm, B = 20cm
<br><br>
Dimensions of the glass<br>
h = 3cm, r = 1cm

Office No-77,5th Floor, Kunal Plaza, Old Mumbai-Pune Highway, Chinchwad, Pune.

You might also like