Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 9 – Statements

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 9 – Statements

Spread the love

Like all programming languages, Python has its specific syntax that you should familiarize yourself with. In this and the couple following lessons we’re going to cover the basic Python syntax, starting, in this lesson, with statements.

Python code consists of statements. A statement is a logical instruction the Python interpreter can understand and execute.

There are 2 types of statements:

– expressions

– assignments

statements

Expressions

An expression is a logical sequence of objects, variables and operators.

We use expressions to perform operations such as addition, concatenation, calling a function, etc.

Here are some examples of expressions:

an arithmetic expression:

3 - 5 ** (2 + 7)

an expression with a function:

len("How many characters?")

By the way, len is a function that returns the length of a string, so the number of characters it consists of. Actually, not just strings, but we’re going to talk about it in more detail later in the course.

Assignments

An assignment is a process in which we create variables, assign values to them or change their values.

The basic syntax of an assignment is:

variable=expression
LHS RHS

LHS – left hand side

RHS – right hand side

The variable is a label that holds the value of the expression. In an assignment the LHS is always a variable.

The RHS may be:

– a literal expression: text = “Hello”

– a variable with an assigned value: greeting = text

– an operation: sum = 5 + 3

QUIZ

1. The 2 types of statements available in Python are:
    A) expressions and assignments
    B) expressions and commands
    C) commands and comparisons
 
2. To perform operations such as addition, concatenation, calling a function we use:
    A) expressions
    B) commands
    C) assignments
 
3. 3 – 5 ** (2 + 7) is:
    A) an assignment
    B) an expression
    C) a comparison
 
4. An example of an assignment is:
    A) len(“abc”)
    B) a > 5
    C) x = 15
 

TRUE OR FALSE?

1) A statement is a logical instruction the Python interpreter can understand and execute.
2) An expression is a logical sequence of objects, variables and operators.
3) In an assignment the RHS is always a variable.
4) We can use a literal expression as the LHS in an assignment.

SOLUTION

QUIZ

1. The 2 types of statements available in Python are:
    A) expressions and assignments
    B) expressions and commands
    C) commands and comparisons
 
2.To perform operations such as addition, concatenation, calling a function we use:
    A) expressions
    B) commands
    C) assignments
 
3. 3 – 5 ** (2 + 7) is:
    A) an assignment
    B) an expression
    C) a comparison
 
4. An example of an assignment is:
    A) len(“abc”)
    B) a > 5
    C) x = 15
 

TRUE OR FALSE?

1) A statement is a logical instruction the Python interpreter can understand and execute.
True
2) An expression is a logical sequence of objects, variables and operators.
True
3) In an assignment the RHS is always a variable.
False
4) We can use a literal expression as the LHS in an assignment.
False

PROJECT

'''
Statements - Project

CHARACTER COUNT
__________________________________________________________
Your task is to write a program that asks the user to enter a string,
assigns the length of the string to a variable and prints a message
informing us how many characters the string consists of.

And here's how you should write this program:

#1. Write code that asks the user to enter a string and saves user input
in a variable called text. The prompt should be:

Enter a text.

#2. Write an expression with the function that returns the length of
a string. Assign the length to a variable char_count.

#3. Write code that will print the following message:

The text "..." consists of ... characters.

The dots will be naturally replaced by the text you enter as user input
and the number of characters in it.

First create a variable called message. Use concatenation to format the
string that is supposed to be printed. The text coming from user input 
should be enclosed in double quotes like above. In order to use double quotes inside a string, the string containing them should be enclosed in single quotes and vice versa.
The formatted string should be assigned to the variable message and then its value should be printed in a separate expression.

So, to sum it up, you should have:
line 1: an expression that formats the message variable using concatenation
and then an assignment where the formatted message is assigned to the 
variable message,
line 2: an expression with a function that prints the message.

There's one more thing we have to keep in mind when using concatenation.
Strings can be only concatenated with other strings, not with integers.
So, just like you did in the previous lesson, the integer char_count
must be converted to a string. Just to remind you, in order to convert
x to a string we use the following syntax:

str(x)

This topic will be covered in more detail when we discuss data type 
conversions, but for now just make use of the str function.

If your input contains white spaces, they also count as characters.
'''

PROJECT SOLUTION

##1. 
text = input("Enter a text: ")

##2.
char_count = len(text)

##3. 
message = 'The text "' + text + '" consists of ' + str(char_count) +' characters.'
print(message)


Spread the love

Leave a Reply