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

2-Variables, Expressions, and Statements

This document introduces fundamental programming concepts like values, variables, expressions, and statements in Python. It discusses data types like integers, floats, and strings. It explains how to assign values to variables and use variables in expressions. It also covers basic operators, taking user input, writing comments, debugging, and some exercises to practice the concepts.

Uploaded by

oning.lagora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

2-Variables, Expressions, and Statements

This document introduces fundamental programming concepts like values, variables, expressions, and statements in Python. It discusses data types like integers, floats, and strings. It explains how to assign values to variables and use variables in expressions. It also covers basic operators, taking user input, writing comments, debugging, and some exercises to practice the concepts.

Uploaded by

oning.lagora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Variables, Expressions, and

Statements
Values and types
• A value is one of the basic things a program works with, like a letter
or a number.
• Some values belong to different types: 2 is an integer, and “Hello,
World!” is a string, so called because it contains a “string” of letters.
• If you are not sure what type a value has, the interpreter can tell you.
Values and types
• Strings belong to the type str and integers belong to the type int. Less
obviously, numbers with a decimal point belong to a type called float,
because these numbers are represented in a format called floating
point.
• What about values like “17” and “3.2”? They look like numbers, but
they are in quotation marks like strings.
• When you type a large integer, you might be tempted to use commas
between groups of three digits, as in 1,000,000. This is not a legal
integer in Python.
Variables
• One of the most powerful features of a programming language is the
ability to manipulate variables. A variable is a name that refers to a
value.
• An assignment statement creates new variables and gives them
values:
Variables
• To display the value of a variable,
you can use a print statement:

• The type of a variable is the type


of the value it refers to.
Variable names and keywords
• Variable names can be arbitrarily long. They can contain both letters
and numbers, but they cannot start with a number.
• It is legal to use uppercase letters, but it is a good idea to begin
variable names with a lowercase letter.
• The underscore character (_) can appear in a name. It is often used in
names with multiple words, such as my_name or
airspeed_of_unladen_swallow.
• Variable names can start with an underscore character, but we
generally avoid doing this unless we are writing library code for others
to use.
Variable names and keywords
• If you give a variable an illegal name, you get a syntax error:

• class is one of Python’s keywords. The interpreter uses keywords to


recognize the structure of the program, and they cannot be used as
variable names.
Statements
• A statement is a unit of code that the Python interpreter can execute.
We have seen two kinds of statements: print being an expression
statement and assignment.
• A script usually contains a sequence of statements. If there is more
than one statement, the results appear one at a time as the
statements execute.
Operators and operands
• Operators are special symbols that represent computations like
addition and multiplication.
• The values the operator is applied to are called operands.
• The operators +, -, *, /, and ** perform addition, subtraction,
multiplication, division, and exponentiation, as in the following
examples:
Operators and operands
• The division operator in Python 2.0 would divide two integers and
truncate the result to an integer:

• To obtain the same answer in Python 3.0 use floored ( //) division.
Expressions
• An expression is a combination of values,
variables, and operators. A value all by itself is
considered an expression, and so is a variable.

• If you type an expression in interactive mode,


the interpreter evaluates it and displays the
result:
• But in a script, an expression all by itself
doesn’t do anything! This is a common source
of confusion for beginners.
Order of operations
• When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence. For mathematical
operators, Python follows mathematical convention. The acronym
PEMDAS is a useful way to remember the rules:
• Parentheses have the highest precedence and can be used to force an
expression to evaluate in the order you want. You can also use parentheses to
make an expression easier to read.
• Exponentiation has the next highest precedence.
• Multiplication and Division have the same precedence, which is higher than
Addition and Subtraction, which also have the same precedence..
• Operators with the same precedence are evaluated from left to right.
Modulus operator
• The modulus operator works on integers and yields the remainder
when the first operand is divided by the second. In Python, the
modulus operator is a percent sign (%). The syntax is the same as for
other operators:
String operations
• The + operator works with strings, but it is not addition in the
mathematical sense. Instead it performs concatenation, which means
joining the strings by linking them end to end.
Asking the user for input
• Sometimes we would like to take the value for a variable from the
user via their keyboard. Python provides a built-in function called
input that gets input from the keyboard.
• Before getting input from the user, it is a good idea to print a prompt
telling the user what to input. You can pass a string to input to be
displayed to the user before pausing for input.
Asking the user for input
• The sequence \n at the end of the prompt represents a newline,
which is a special character that causes a line break. That’s why the
user’s input appears below the prompt.
• If you expect the user to type an integer, you can try to convert the
return value to int using the int() function:
Comments
• It is a good idea to add notes to your programs to explain in natural
language what the program is doing. These notes are called
comments, and in Python they start with the # symbol.
Choosing mnemonic variable names
• We call these wisely chosen variable names “mnemonic variable
names”. The word mnemonic means “memory aid”. We choose
mnemonic variable names to help us remember why we created the
variable in the first place.
Debugging
Exercises
• Exercise 2.1 - Write a program that uses input to prompt a user for
their name and then welcomes them.
• Exercise 2.2 - Write a program to prompt the user for hours and rate
per hour to compute gross pay.
• Exercise 2.3 - Write a program which prompts the user for a Celsius
temperature, convert the temperature to Fahrenheit, and print out
the converted temperature.

You might also like