Professional Documents
Culture Documents
2) Variables
2) Variables
Lecture 2: Variables
vision.seecs.edu.pk
Recap - week1
• Printing
Contents
• User Input
• Comments
• Operators
• Variables
• Typecasting
• Strings
User Input
Getting input from users:
• The input function takes one argument which should be a question to
ask the user as a string (i.e. in double quotes)
• We can then assign the user’s response, to a variable to use later
4
User Input
• For consistency, the input command will always return a string even if
the user input is better suited for a different type
• Because of this, you may need to convert the variable storing the user
input to a different type
5
Comments
Comments are used:
• to explain Python code.
• to make the code more readable.
• to prevent execution when running code
Single-line comment:
• Insert # for comment
Multi-line comment:
https://www.tutorialspoint.com/python3/python_basic_operators.htm
Operators
• Assignment operators: used to assigning values to variables
Operators
• Comparison operators: compare the values on either side of them and decide the relation among
them
Variables
• Variables let you hold information to be used later in your code
• A variable is essentially a way of naming a particular value so we can
reference it later
• We assign a value to a variable using the equals symbol
• Here we made two variables called ‘name’ and ‘age’ with the values
“David” and 24, respectively
• Once you’ve created a variable it will continue to exist until you either
delete it or close your browser
Variables
• Once we have assigned a variable a value, we can use it in our code
by simply typing its name
12
Manipulating Variables
1. Python will only remember the last value that you assigned to a
variable
• Lists/Tuples/Sets
• Dictionaries
Variable Types
• int() - constructs an integer number from an integer literal, a float literal (by
removing all decimals), or a string literal (ONLY if string is a whole number)
• float() - constructs a float number from an integer literal, a float literal or a
string literal (only if string is a float or an integer)
• str() - constructs a string from a wide variety of data types, including strings,
integer literals and float literals
String Variables
• Multi-line String:
• Multi-line string are created using 03 double quotes “”” or 03 single quotes ‘’’
a = ""“ Python is widely used for web development, a = ""“ Python is widely used for web development,
scientific computing, data analysis, artificial scientific computing, data analysis, artificial
intelligence, and more. intelligence, and more.
This makes it a versatile language to learn and opens This makes it a versatile language to learn and
up many job opportunities.""" opens up many job opportunities."""
print(a) print(a)
Output:
Python is widely used for web development,
Output: scientific computing, data analysis, artificial
SyntaxError intelligence, and more.
• Concatenation:
• Create two string variables. What happens when we add them together
using `+` and print the result?
string_one = "I am reading" string_one = "I am reading"
string_two = " a great book!" string_two = " a great book!"
string_three = string_one + string_two string_three = string_one + string_two
print(string_three) x=4
y=string_three + x
print(y)
Output: I am reading a great book!
String Variables
• Replication:
• Create two string variables. What happens when we multiply a string by
an integer? [ >> Replicate the given string integer times]
print(“hello ” * 5) s = "hello"
print('s' * 5)
Output: Output:
hello hello hello hello hello sssss
Length of String
• The len() command is versatile (also work with lists, will see later)
• In fact, you can pass a string as the input to len() and it will return the
number of characters (including spaces) in that string
22
String Operations
• String concatenation +
• String replication *
• String starting index [0]
• String slicing [ ] and [:]
23
Variables – Self Practice 1
• Create two variables called ‘name’ and ‘age’, giving them the value of your
name and age respectively
• Get Python to print these in the following format:
Hi, my name is ______ and I am ______
x = 20
print(x)
print(2*x)
print(x-1)
Output:
20
40
19
25
Variable - Self Practice 2
• Create a variable `pi` with the value 3.14 and a variable `radius` with
any value of your choosing
• Use these to print the circumference and area of a circle with radius
equal to the value of `radius`
• You may need the following formulae:
• use the round() function to round your answer to 2 decimal places. This
takes to inputs, a number to round and the number of decimal places to
round to (separated by a comma, remember!)
26
Variable - Solution
27
Manipulating Variables - Self Practice 3
• Run the following lines of code:
28
Manipulating Variables - Solutions
• The input() function creates an input box immediately after the question
rather than on a new line. Try adding the characters ‘\r\n’ to the end of
your question to fix this
30
User Input - Solution