0% found this document useful (0 votes)
8 views

L02P

The document covers the fundamentals of variables and data conversions in Python, explaining how variables represent values in memory and can be created through assignment statements. It details the rules for naming variables, common data types such as int, float, and str, and how to read input from the keyboard while converting data types using built-in functions like int(), float(), and str(). Additionally, it discusses the behavior of mixed-type expressions in mathematical operations.

Uploaded by

mohaalhabshi515
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)
8 views

L02P

The document covers the fundamentals of variables and data conversions in Python, explaining how variables represent values in memory and can be created through assignment statements. It details the rules for naming variables, common data types such as int, float, and str, and how to read input from the keyboard while converting data types using built-in functions like int(), float(), and str(). Additionally, it discusses the behavior of mixed-type expressions in mathematical operations.

Uploaded by

mohaalhabshi515
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
You are on page 1/ 25

PYTHON

Variables and Data Conversions

Salem Belgurzi
Topics

 Variables

 Reading (Interpreting) Input from the Keyboard

 Data Conversions
Variables
 Variable: name that represents a value stored in the computer
memory
 Used to access and manipulate data stored in memory
 A variable references the value it represents

 Assignment statement: used to create a variable and make it


reference data
 General format is variable = expression
 Example: age = 29
 Assignment operator: the equal sign (=)
Variables

 Each variable has a name


and holds a value.

 A variable is similar to a
parking space in a parking
garage.

 The parking space has an


identifier (such as “J 053”),
and it can hold a vehicle.
Variables

This means that variable is assigned value. i.e., after the assignment,
variable "contains" value.

The equals sign is NOT algebraic equality.


It causes an action! The expression on the right is evaluated and the
result is assigned to the variable on the left.
>>> x = 1 7 . 2
>>> y = - 3 9
>>> z = x * y - 2
>>> p r i n t ( z )
-672.8
Variables
Unlike some programming languages, Python variables do not have fixed
data types.

# C code

int x = 17; / / variable x has type int

x = 5.3; // i l l e g a l
# Python code
x = 17 # x gets int value 17
x = 5.3 # x gets float value 5.3
Variables
You can create a new variable in Python by assigning it a value. You
don’t have to declare variables' types, as in many other programming
languages.
>>> x= 3 # creates x, assigns int

>>> print( x)
3
>>> x = " abc" # re - assigns x a string
>>> print( x)
abc
>>> x = 3.14 # re - assigns x a float
>>> print( x)
3.14
>>> y= 6 # creates y, assigns int
>>> x* y # uses x and y
18.84
Variables
 In assignment statement, variable receiving value must be on left
side

 A variable can be passed as an argument to a function


 Variable name should not be enclosed in quote marks

 You can only use a variable if a value is assigned to it


Variable Naming Rules
 Rules for naming variables in Python:
 Variable name cannot be a Python key word
You can’t use reserved words; these have a special meaning to Python and cannot
be variable names.
 Variable name cannot contain spaces
 First character must be a letter or an underscore
Variable names must begin with a letter or underscore (_) character. After that,
use any number of letters, underscores, or digits.
 After first character may use letters, digits, or underscores
 Variable names are case sensitive
Case matters: "score" is a different variable than "Score."
Python Keywords
 Python Reserved Words.
 Also known as Keywords.

and, as, assert, break, class, continue, def ,eslaF ,tpecxe ,esle ,file ,led ,
,ro ,ton ,enoN ,lacolnon ,adbmal ,si ,ni ,tropmi ,fi ,labolg ,morf ,rof ,yllanif
dleiy ,htiw ,elihw ,yrt ,eurT ,nruter ,esiar ,ssap

 PyCharm, and other IDEs display reserved words in a different color


to help you recognize them.
Naming Variables
Naming Variables
Naming Variables
 In addition to the rules, there are also some conventions that
programmers follow :
 Variable names shall begin with a lowercase letter.(It is legal to use
uppercase letters, but it is a good idea to begin variable names with a
lowercase letter)
 Choose meaningful names that describe how the variable is used. This
helps with program readibility.
 Use underscores to separate multiple words (The underscore
character, _, can appear in a name. It is often used in names with
multiple words, such as my_name or num_hrs_partA)
Naming Variables
If you give a variable an illegal name, you get a syntax error:

>>> 76trombones = 'big parade’


SyntaxError: invalid syntax

>>> more@ = 1000000


SyntaxError: invalid syntax

>>> class = 'Advanced Theoretical Zymurgy’


SyntaxError: invalid syntax
Common Python Data Types
Numeric Data Types, Literals, and the str Data
Type
 Data types: categorize value in memory
 e.g., int for integer, float for real number, str used for storing strings in
memory

 Numeric literal: number written in a program


 No decimal point considered int, otherwise, considered float

 Some operations behave differently depending on data type


Data Types
The type function

>>> x = 17
>>> t ype ( x)
<cl ass ’ i nt ’ > • Class is another name for data type.
>>> y = -20.9 • Data type is a classification
>>> t ype ( y) • "What kind of thing is the value this
<cl ass ’ f l oat ’ > variable refers to?"
>>> t ype ( 20)
<cl ass ’ i nt ’ >
>>> t ype ( ’ abc ’ )
<cl ass ’ st r ’ >
Data Types
 Three data types we will use in many of our Python programs are:
 int: signed integers (whole numbers)
 Computations are exact and of unlimited size
 Examples: 4, -17, 0
 float: signed real numbers (numbers with decimal points) Large
range, but fixed precision
 Computations are approximate, not exact Examples: 3.2, -9.0,
3.5e7
 str: represents text (a string)
 We use it for input and output We’ll see more uses later
Examples: "Hello, World!", ’abc’
 These are all immutable. The value cannot be changed.
Reading Input from the Keyboard
 Most programs need to read input from the user

 Built-in input function reads input from keyboard


 Returns the data as a string
 Format: variable = input(prompt)
 prompt is typically a string instructing user to enter a value
Reading Numbers with the input Function
 input function always returns a string

 Three primary built-in functions that allow you to convert data from
one type to another :
 int(item) converts item to an integer
 float(item) converts item to a float
 str(item) converts item to a string
Conversion Functions– int()
int(): This function is used to convert a value to an integer data type.

num_str = “9”
num_int = int(num_str)

print(num_int) # Output: 9
print(type(num_int)) # Output: <class 'int’>

In the above example, the string “9" is converted to an integer using


the int() function. The resulting value is 9, and its data type is <class
'int’>.
Conversion Functions – float()
float(): This function is used to convert a value to a floating-point data
type.

num_str = “4.15"
num_float = float(num_str)

print(num_float) # Output: 4.15


print(type(num_float)) # Output: <class 'float’>

In this example, the string “4.15" is converted to a float using


the float() function. The resulting value is 4.15, and its data type
is <class 'float'>.
Conversion Functions – str()
str(): This function is used to convert a value to a string data type.

num_int = 40
num_str = str(num_int)

print(num_str) # Output: "40"


print(type(num_str)) # Output: <class 'str’>

In this example, the integer 40 is converted to a string using


the str() function. The resulting value is "40", and its data type
is <class 'str'>.
Mixed-Type Expressions and Data Type
Conversion
 Data type resulting from math operation depends on data types of
operands
 Two int values: result is an int
 Two float values: result is a float
 int and float: int converted to float, result of the operation is a
float
 Mixed-type expression

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy