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

1 Python Values and Variables

The document covers fundamental concepts of Python programming, focusing on values and variables, including integers, strings, and floating-point numbers. It explains variable assignment, identifiers, user input, control codes within strings, and string formatting. Additionally, it discusses the print function and how to control its output, including the use of keyword arguments and multi-line strings.

Uploaded by

maha maalej
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

1 Python Values and Variables

The document covers fundamental concepts of Python programming, focusing on values and variables, including integers, strings, and floating-point numbers. It explains variable assignment, identifiers, user input, control codes within strings, and string formatting. Additionally, it discusses the print function and how to control its output, including the use of keyword arguments and multi-line strings.

Uploaded by

maha maalej
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Python

Values and Variables


Values and Variables
1. Integer and String Values
2. Variables and Assignment
3. Identifiers
4. Floating-point Numbers
5. Control Codes within Strings
6. User Input
7. Controlling the print Function
8. String Formatting
9. Multi-line Strings
1. Integer and String Values
(1/3)
Integers are whole numbers, which means they have no fractional parts, and they
can be positive, negative, or zero.
Examples of integers include 4, -19, 0, and -1005.
In contrast, 4.5 is not an integer, since it is not a whole number.
The Python statement
>>print(4)
prints the value 4.
1. Integer and String Values
(2/3)
A string is a sequence of characters. Strings most often contain nonnumeric
characters:
>>> "Fred"
'Fred'
>>> 'Fred'
'Fred'
Python recognizes both single quotes (') and double quotes (") as valid ways to
delimit a string value.
The word delimit means to determine the boundaries or limits of something.
1. Integer and String Values
(3/3)
An expression’s type is denoted as its class.
 The built in type function reveals the type of any Python expression:
>>> type(4)
<class 'int'>
>>> type('4')
<class 'str’>
 The expressions 4 and '4' are different.
 4 is an integer expression and '4’ is a string expression.
2. Variables and
Assignment (1/2)
>>x = 10
>>print(x)
uses a variable to store an integer value and then prints the value of the variable.
x = 10 This is an assignment statement.
An assignment statement associates a value with a variable. The symbol = is known as the assignment operator.
This statement binds the variable named x to the value 10.
We can reassign different values to a variable as needed
>>x = 10
>>print(x)
>>x = 20
>>print(x)
2. Variables and
Assignment (2/2)
 A tuple is a comma-separated list of expressions.
>>x, y, z = 100, -45, 0
>>print('x =', x, ' y =', y, ' z =', z)
produces
x = 100 y = -45 z = 0
3. Identifiers (1/3)
 Python has strict rules for variable names.
 A variable name is one example of an identifier.
 An identifier is a word used to name things.
 One of the things an identifier can name is a variable.
 Identifiers name can be functions, classes, and methods.
 Python keywords
and del from None try as elif global nonlocal True for

assert else if not while break except import or with lambda

class False in pass yield continue finally is raise def return


3. Identifiers (2/3)
None of the following words are valid identifiers:
 sub-total (dash is not a legal symbol in an identifier)
 first entry (space is not a legal symbol in an identifier)
 4all (begins with a digit)
 *2 (the asterisk is not a legal symbol in an identifier)
 class (class is a reserved word)
3. Identifiers (3/3)
 Python is a case-sensitive language.
 This means that capitalization matters. if is a reserved word, but
none of If, IF, or iF is a reserved word.
 Identifiers also are case sensitive; the variable called Name is
different from the variable called name.
 Note that three of the reserved words (False, None, and True) are
capitalized.
4. Floating-point Numbers
(1/4)
>>> x = 5.62
>>> x
5.62
>>> type(x)
<class 'float’>
4. Floating-point Numbers
(2/4)
 We can express floating-point numbers in scientific notation.
 Since most programming editors do not provide superscripting
and special symbols like , Python slightly alters the normal scientific
notation.
 The number 6.022*1023 is written 6.022e23.
 The number to the left of the e (we can use capital E as well) is
the mantissa, and the number to the right of the e is the exponent
of 10.
 As another example, -5.1*10-4 is expressed in Python as -5.1e-4.
4. Floating-point Numbers
(3/4)
The type of any literal expressed scientific notation always has type float;
for example, the Python expression 2e3 is a float, even though conceptually we may consider it the same as integer
the 2,000.
Unlike floating-point numbers, integers are whole numbers and cannot store fractional quantities.
We can convert a floating-point to an integer in two fundamentally different ways:
• Rounding
>>> round(28.71)
29
>>> round(19.47)
19
• Truncation
>>> int(28.71)
28
>>> int(19.47)
19
4. Floating-point Numbers
(4/4)
The round function accepts an optional argument that produces a floating-point rounded to fewer decimal places.
The additional argument must be an integer and specifies the desired number of decimal places to round.
>>> x
93.34836
>>> round(x)
93
>>> round(x, 2)
93.35
>>> round(x, 3)
93.348
The second argument to the round function may be a negative integer:
>>> x = 28793.54836
>>> round(x, -1)
28790.0
>>> round(x, -2)
28800.0
5. Control Codes within
Strings (1/2)
 The characters that can appear within strings include letters of the alphabet (A-Z, a-z),
digits (0-9), punctuation (., :, ,, etc.), and other printable symbols (#, &, %, etc.).

There are some characters called control codes


The backslash symbol (\) signifies that the character that follows it is a control code, not a
literal character.
The string '\n' thus contains a single control code.
\n represents the newline control code,
\t for tab,
\f for a form feed (or page eject) on a printer,
\b for backspace,
and \a for alert (or bell).
The \b and \a do not produce the desired results in the interactive shell, but they work
properly in a command shell.
5. Control Codes within
Strings (2/2)
>>>filename = 'C:\\Users\\rick’
>>>print(filename)
C:\Users\rick
6. User Input (1/3)
The print function enables a Python program to display textual information to
the user.
 Programs may use the input function to obtain information from the user.
The simplest use of the input function assigns a string to a variable:
x = input()

>>>x = input()
>>>print('Text entered:', x)
My name is Rick
Text entered: My name is Rick
6. User Input (2/3)
The input function produces only strings, but we can use the
int function to convert a properly formed string of digits into an
integer.

>>>x = input()
>>>y = input()
>>>num1 = int(x)
>>>num2 = int(y)
>>>print(num1, '+', num2, '=', num1 + num2) 2
17
2 + 17 = 19
6. User Input (3/3)
For float numbers:
>>> num = int(float(input('Please enter a number: ')))
Please enter a number: 3
>>> num
3
>>> num = int(float(input('Please enter a number: ')))
Please enter a number: 3.4
>>> num
3
What if you wish to round the user’s input value instead of truncating it?

>>> num = round(float(input('Please enter a number: ')))


Please enter a number: 3.7
>>> num
4
7. Controlling the print
Function (1/4)
print('Please enter an integer value:', end=‘’)
 The expression end='' is known as a keyword argument.
 The term keyword here means something different from the term keyword used to mean a
reserved word.
 We defer a complete explanation of keyword arguments until we have explored more of the
Python language.
 For now it is sufficient to know that a print function call of this form will cause the cursor to
remain on the same line as the printed text.
 Without this keyword argument, the cursor moves down to the next line after printing the
text.
 The print statement print('Please enter an integer value: ', end='')
means
print(end='Please enter an integer value: ')
7. Controlling the print
Function (2/4)
The statement
>>>print('Please enter an integer value:')
is an abbreviated form of the statement
>>>print('Please enter an integer value:', end='\n’)

the statement
print()
is a shorter way to express
print(end='\n')
7. Controlling the print
Function (3/4)
>>>print('A', end=‘’)
>>>print('B', end=‘’)
>>>print('C', end=‘’)
ABC
7. Controlling the print
Function (4/4)
By default, the print function places a single space in between the items it prints.
print uses a keyword argument named sep to specify the string to use insert between items.
The name sep stands for separator.
The default value of sep is the string ' ', a string containing a single space.

>>>w, x, y, z = 10, 15, 20, 25


>>> print(w, x, y, z)
10 15 20 25
>>> print(w, x, y, z, sep=',’)
10,15,20,25
>>> print(w, x, y, z, sep=‘’)
10152025
>>> print(w, x, y, z, sep=':’)
10:15:20:25
>>> print(w, x, y, z, sep='-----’)
10-----15-----20-----25
8. String Formatting (1/2)
print(0, 10**0)
print(1, 10**1)
print(2, 10**2)
print(3, 10**3)
print(4, 10**4)
print(5, 10**5)

0 1
1 10
2 100
3 1000
4 10000
5 100000
8. String Formatting (2/2)
print('{0} {1}'.format(0, 10**0))
print('{0} {1}'.format(1, 10**1))
print('{0} {1}'.format(2, 10**2))
print('{0} {1}'.format(3, 10**3))
print('{0} {1}'.format(4, 10**4))
print('{0} {1}'.format(5, 10**5))
Has the same formatting as previous slide
9.Multi-line Strings
 A Python string ordinarily spans a single line of text. The following statement is illegal:
x = 'This is a long string with
several words’
 Python provides way to represent a string’s layout more naturally within source code, using triple
quotes.
 The triple quotes (''' or """) delimit strings that can span multiple lines in the source code.
>>>x = '''
This is a multi-line
string that goes on
for three lines! This is a multi-line
‘‘’ string that goes on
for three lines!
>>>print(x)
Python
Values and Variables

You might also like