0% found this document useful (0 votes)
7 views21 pages

Python Basics: Variables and Input

The document introduces the importance of coding in today's technology-driven world, highlighting its relevance for future opportunities. It covers basic concepts such as variables, their naming conventions, data types, and the use of the input() function in Python. Additionally, it includes practice exercises to reinforce understanding of these concepts.

Uploaded by

Pratinav Tewari
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)
7 views21 pages

Python Basics: Variables and Input

The document introduces the importance of coding in today's technology-driven world, highlighting its relevance for future opportunities. It covers basic concepts such as variables, their naming conventions, data types, and the use of the input() function in Python. Additionally, it includes practice exercises to reinforce understanding of these concepts.

Uploaded by

Pratinav Tewari
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

Python 101

Importance of Coding

As technology keeps growing, coding will become even more


crucial. Almost everything around us, from our smartphones
to smart homes and self-driving cars, relies on coding. So, if
you know how to code, you'll have lots of opportunities in the
future to work on amazing projects and solve important
problems.
Review
Review

Which one is correct?


print(“Hello”)

print(‘Hello’)
Review

print(“3+4”)
print(3+4)
Print Statement

What will this print?

print(hi)
Review
Variables

A variable is like a container that can hold Ex: x = 5


different things, like numbers or words. It's x = “hello”
a way for the computer to remember and
use information. Just like you can label a
box with a name and put things inside, a
variable has a name and stores data that
can be changed or used later in the
program.
Variable naming

Naming variables appropriately is Ex: If you need a


important because it helps us understand variable for
what the variable represents. It's like giving someone’s age you
things the right name so we know what would do age = 16
they are. When we choose clear and or if you want a
variable for an
descriptive names for variables, it
apple it would be
becomes easier to read and understand fruit = “apple”
our code. It's like labeling items properly so
we can find and use them easily.
Variable naming

Name cannot be represented as an Ex:


integer, and age cannot be represented as my name = ‘nandika is
wrong it would be
string. In most programming languages, my_name = ‘nandika
variable names are case-sensitive. It's
common practice to use lowercase letters
for variable names. You also have to use
underscores after word instead of using
spaces. You also don’t start with a
uppercase letter
Variable
hi = ‘hello’

Variable print(hi)
Types of variables
Integers (int) are whole numbers without any decimal places.
They can be positive or negative. Examples of integers are -3, 0,
and 5.

Floating-point numbers (float) are numbers that include


decimal places. They can be positive or negative. Examples of
floating-point numbers are 3.14, -0.5, and 2.75.

Strings (str) are sequences of characters enclosed in quotation


marks (either single or double). Examples of strings are "Hello",
and "12345".

* You can find the data type of a variable by doing type(variable) ex:
Age = 16
type(age)
print(type(age))
Variables
Will a save as 5 or 6?

a=5
a=6
print(a)
variables
It’ll save as 6 because if you set a variable to 2 different things it will save as the
last line you wrote so in this case because a = 6 is the last line you wrote a = 6.
it will save and display the latest assigned value
Input

In Python, the input() function is used to


get information or data from the user. It
allows you to ask the user a question or
prompt them for some input, and they can
type in their response using the keyboard.

In general in programming input is the


information that the computers take in and
output is the information the computer
gives you back. Ex: In vending machines
the input is the money you provide to the
vending machine, and the output is the bag
of chips you receive.:
Input
Printing a variable

You can print a variable like we did previously you can put the variable name inside
the round brackets. print(variable_name) But sometimes you want to print text before
the variable. For example you might want to have have a variable age where we age
the user their age and then we want to print ‘I am’’ age “years old’

age = input(“How old are


you?”)
print(‘I am ‘ + age + ‘years
old’)
Practice
1. What is the datatype of x = 10

2. What is the datatype of x = ‘3.14’

3. What is the datatype of x = 10.6

4. Write a variable to store your age

5. Write a variable to store your name


Practice
1. Declare two variables num1 and num2.

2. Assign the values 10 and 5 to num1 and num2, respectively.

3. Calculate the sum of num1 and num2 and store the result in a

variable called sum.

4. Display the calculated sum using the print() function.


Practice
1. Declare two variables length and width.

2. In both variables use input() and ask the user what is the length

and what is the width.

3. Create a new variable called area and find the area by

multiplying length and width

4. Display the calculated sum using the print() function.

You might also like