0% found this document useful (0 votes)
19 views26 pages

Lec 3

Uploaded by

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

Lec 3

Uploaded by

m.asifshaikh80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Program: National Vocational and Technical Training Commission (NAVTTC)

Course: Advance Python Programming & Applications


Session: Batch I
Instructor: Engr. M. Asif Shaikh

Week 2

Lecture 3
Comments, Data Types, Variables

3/5/2024 Week2 \ Lec3 1


Brainstorming

What is instruction

3/5/2024 Week2 \ Lec3 2


Head Lines
• Comments

• Data Types

• Variables

3/5/2024 Week2 \ Lec3 3


Comments
• Comments can be used to explain Python
code.

• Comments can be used to make the code


more readable.

• Comments can be used to prevent execution


when testing code.
3/5/2024 Week2 \ Lec3 4
• Comments starts with a #, and Python will
ignore them
 #This is a comment
print(“Salam…")

 print("Hello, World!") #This is


a comment

 #print("Hello, World!")
3/5/2024 Week2 \ Lec3 5
• Python does not really have a syntax for
multiline comments.

• To add a multiline comment you could insert a


# for each line:

3/5/2024 Week2 \ Lec3 6


#This is a comment
#written in
#more than just one line
print("Hello, World!")

3/5/2024 Week2 \ Lec3 7


Data Types
Built-in Data Types

• In programming, data type is an important


concept.

• Variables can store data of different types, and


different types can do different things.

• Python has the following data types built-in by


default, in these categories:
3/5/2024 Week2 \ Lec3 8
3/5/2024 Week2 \ Lec3 9
• In Python, the data type is set when you
assign a value to a variable:

• You can get the data type of any object by


using the type() function:
x = 5
print(type(x))

3/5/2024 Week2 \ Lec3 10


3/5/2024 Week2 \ Lec3 11
• There may be times when you want to specify
a type on to a variable.

• This can be done with casting.

• Casting in python is therefore done using


constructor functions:

3/5/2024 Week2 \ Lec3 12


• int() - constructs an integer number

• float() - constructs a float number

• str() - constructs a string

3/5/2024 Week2 \ Lec3 13


Variables
• Variables are containers for storing data values
in memory.

• Python has no command for declaring a


variable.

• A variable is created the moment you first


assign a value to it.
3/5/2024 Week2 \ Lec3 14
x = 5

y = "John“

print(x)

print(y)

3/5/2024 Week2 \ Lec3 15


• Variables do not need to be declared with any
particular type, and can even change type after
they have been set.

x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)

3/5/2024 Week2 \ Lec3 16


• A variable can have a short name (like x and y)
or a more descriptive name (age, carname,
total_volume).

• Rules for Python variables:

3/5/2024 Week2 \ Lec3 17


 A variable name must start with a letter or the underscore
character

 A variable name cannot start with a number

 A variable name can only contain alpha-numeric characters and


underscores (A-z, 0-9, and _ )

 Variable names are case-sensitive (age, Age and AGE are three
different variables)

 A variable name cannot be any of the Python keywords.


3/5/2024 Week2 \ Lec3 18
Exercise (Lab Tasks)
1. Create a variable named x, and assign it a value of 20

2. We have a variable named x. Assign it a string value of "Hello


World".

3. Create two variables named x and y, and assign them with


values 10 and 20 respectively.

4. We have a variable named x which is assigned a value


of 'apple'. Print the variable x to standard output.

3/5/2024 Week2 \ Lec3 19


5. initialize three variables x, y, and z with the
same value of 'apple' in a single statement.

6. Create two variables named x and y, and assign


them with values 10 and 20 respectively.

7. We have a print statement that ought to print


the data type of x. Insert the built-in function
that returns the type of x, if x=“apple”
3/5/2024 Week2 \ Lec3 20
8. What would be the type of x if
x = 3.14

print(type(x))

3/5/2024 Week2 \ Lec3 21


9. We have a variable named x which is
assigned an integer value 6, and a
variable y which is assigned with a floating
point number 2.95. We find the sum of these
two numbers, and store the result in
variable z. Then we print the data type of
variable z using type() built-in function. What
would be the type of z?

3/5/2024 Week2 \ Lec3 22


10)Type the built-function in the following,
where we read an input from user, and assign
the read input to variable x.

x=input()
print(x)

3/5/2024 Week2 \ Lec3 23


11.Read a greeting from user input and print it
with your name comes first.

12.Read your age from input and print message


I am xx years old

3/5/2024 Week2 \ Lec3 24


What we will learn next.
• We will discuss followings in next class.

operators

3/5/2024 Week2 \ Lec3 25


Useful Links

https://www.youtube.com/watch?
v=X6TcB0DNLE8

END OF LECTURE
3/5/2024 Week2 \ Lec3 26

You might also like