0% found this document useful (0 votes)
17 views6 pages

Python Variables and Data Types Guide

Uploaded by

Shelmi ISAS
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)
17 views6 pages

Python Variables and Data Types Guide

Uploaded by

Shelmi ISAS
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

# print a string number = float(input("Enter a

print('Harry Potter') number: "))


# print a number print(type(number))
print(50)
number = 400 Comments are hints that we can
# print a variable add to our program to make our
print(number) code easier to understand.
goals = '100' # Program to take the user's name
# strings and variables in the same name = input('Enter your name')
line print(name)
print('Ronaldo has reached ', goals,
' goals') # declare a variable
name = 'John'
# take user input # print name
name = input() print(name) # John
print(name)
number1 = 10
# displaying prompt message number2 = 15
before taking input sum = number1 + number2
name = input("Enter name: ") print('The sum is:', sum)
print(name) print('The product is:', product)

# print the type of input. '''This program takes an input from


number = input("Enter a number: the user
") and prints it'''
print(type(number)) name = input('Enter your name: ')
print(name)
# take input
number = input("Enter a number:
") Python Variables
# convert numeric string to integer In this tutorial, you will learn about
number = int(number) Python variables with the help of
examples.
# take input and convert it to int In programming, a variable is a
number = int(input("Enter a container that is used to store
number: ")) values. For example,
age = 25
# take input and convert it to float
try with string input also
Assign value to a variable
Here,
 age is a variable,
 25 is the value stored in the variable age, and
 = is the operator used to store value to a variable.
Variables can store different types of data types. In this example, the
variable age is storing an integer type.
To learn more about data types, visit Python data types.

Print Variables in Python


We can simply print a variable using the print() function. For example,
age = 25
# print the variable age
print(age) # 25
Note that if we use the quotation '' in the print statement, Python considers it
as a string. For example,
age = 25
# variable with quotation
# prints the string
print('age')
# Output: age

Declare Variables Before Use


We cannot use a variable before defining them. For example,
age = 25
# use the name variable without defining it
# error
print(name)
print(age)
Output
NameError: name 'name' is not defined

Change Value of a Variable


In Python, we can change the value stored in a variable. For example,
city = 'New York'
print(city) # New York
# change the value of city
city = 'California'
print(city) # California
Here, the value of the variable city is changed from 'New York' to 'California'.
Note: You do not have to explicitly define the variable type in Python. When
we assign 'New York' to the variable city, it automatically declares city as a
string.

Assign One Variable To Another


We can also assign one variable to another. For example,
city = 'New York'
destination_city = 'California'
# assign one variable to another
city = destination_city
print(city) # California
Here, we have assigned the value of destination_city to city using
the = operator.

Rules of Naming Variables in Python


Here are the rules for naming variables:
 A variable name can consist of alphabets, digits, and an underscore.
 Variables cannot have special symbols like $, @, #, etc.
 Variable names cannot begin with a number. For example, 1name.
By the way, we should always try to give meaningful variable names. This
makes your code easier to read and understand.
Good, Bad and Illegal Variable Names

Illegal Variable Bad Variable Good Variable

date time dati dateTime

city-name cn city_name

20n numb number

s@lary sal salary

Python Data Types


In this tutorial, you will learn about difference types of data that we can use
in Python with the help of examples.
There are different types of data that we store in variables. These different
types of data are known as data types. For example,
x=5
y = 'Python'
Here, x is an integer data type and y is a string data type.

Different Python Data Types

Data Types Example

Integer x=7

Float x = 7.5

String x = 'Programiz'

Boolean x = False

Complex x = 5j

List x =['Python', 'C', 'Java']

Tuple x = ('Python', 'C', 'Java')

Set x = {'Python', 'C', 'Java')

Dictionary x = {'name' : 'Tim', 'age' : 56}

Since everything is an object in Python programming, data types are classes


and variables are instances (object) of those classes.
For x = 5, x is an instance of class int.
Note: We use type() method to identify the data type of the variable. For
example,
x=5
print(type(x))
# Output: <class 'int'>

Commonly Used Python Data Types


Some of the commonly used data types in Python are:
 Python Numbers (int, float)
 Python Strings str
Int
The integer data type int represents positive or negative whole numbers(i.e.
numbers without fraction or decimal). For example,
# positive number
x = 45
print(type(x)) # <class 'int'>
# negative number
x = -3
print(type(x)) # <class 'int'>
Float
The floating point float type represents decimal or fractional numbers. For
example,
x = 5.5
print(type(x)) # <class 'float'>
x = 0.002
print(type(x)) #<class 'float'>
String
The string data type (str) represents alphanumeric characters. For example,
x = 'Car'
print(type(x)) # <class 'str'>
x = 'abc12'
print(type(x)) # <class 'str'>
x = 'Python Programming'
print(type(x)) # <class 'str'>

Other Python Data Types


Other data types that are commonly used in Python are:
Lists
The list type allows us to store multiple values. The elements of a list are
inside square [] brackets. For example,
my_list = [2, 'python', 4.5]
To learn more about list, visit Python List.
Tuples
Tuple is also used to store elements of multiple data types similar to lists.
One important difference between a tuple and a list is that lists are mutable
(changeable), whereas tuples are immutable (unchangeable). The tuple is
created using () parenthesis. For example,
my_tuple = ('USA', 'jake', 54)
To learn more about list, visit Python Tuple.
Sets
Set is an unordered collection of items. Also, a set cannot contain duplicate
elements. The elements of set are inside curly braces {}. For example,
my_set = {1, 3, 'jimmy'}
To learn more about list, visit Python Set.
Dictionary
The dictionary is an ordered collection that stores elements in key/value
pairs. For example,
my_dict = {'name' : 'Jake' , 'age' : 15}
The elements of a dictionary are accessed using the keys. To learn more
about list, visit Python Dictionary.
Booleans
The boolean data type represents one of two values: either True or False. For
example,
x = True
print(type(x)) # <class 'bool'>
To learn more, visit Python Booleans
None
In Python, None means no value (lack of value) For example,
x = None

You might also like