# Variable and Data Types
# Variable
A variable is a container that stores value.
Unlike some other languages, there is NO need to declare the type of a variable in Python.
For example, to create a variable called number
of type int in Java
int number;
number = 5;
2
But in Python, we can simply do
number = 5
And by the way, Python does NOT require semi-colons at the end of each statement.
We can use any characters of any written languages to name the variables.
π = 3.14159
jalapeño = "a hot pepper"
好吃 = "delicious"
2
3
# Number
Python can handle numbers with or without fractions. There are integer type (int
) and decimal type (float
).
int_number = 5
float_number = 5.5
2
Python also knows about i² = -1
Python also has a complex
type to deal with complex numbers.
complex_number = 1 + 2j
another_complex_number = complex(1, 2)
2
# Text
Python use a type str
to handle text or strings. Single/double quotes can be used to represent a string value.
one_string = 'hello'
another_string = "world"
2
When there is a lot of text, use triple quotes to denote a blob of text
a_large_string = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse pharetra convallis blandit.
Etiam egestas porta vehicula.
Aliquam est lorem, semper quis turpis vel, finibus iaculis eros.
Donec lobortis sollicitudin luctus.
Nullam faucibus semper lectus ac lobortis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Nam eget urna id diam scelerisque porta.
Sed placerat porttitor quam.
'''
another_large_string = """
Donec volutpat turpis in porta elementum.
Proin sed suscipit elit.
Donec eu dolor dignissim, iaculis tellus nec, hendrerit libero.
Aenean massa velit, eleifend in ultricies sed, luctus nec purus.
Integer non lacus consectetur, auctor ipsum in, ullamcorper orci.
Fusce nec erat a velit efficitur posuere.
Vestibulum non felis mi.
Vivamus ut malesuada enim, eget condimentum mi.
Donec venenatis volutpat blandit.
Praesent rutrum maximus urna scelerisque vestibulum.
Nulla aliquet porta nisi, quis vestibulum erat facilisis vitae.
Cras hendrerit arcu in lorem viverra eleifend.
"""
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Boolean
A boolean value can be either true (1) or false (0). It can be used to represent whether a condition is met or not.
true_value = True
false_value = False
2
# Some Basic Built-in Functions
As mentioned from previous section, Python comes with many useful functions.
We can display the value of a variable in command line using print()
print(jalapeño)
We can check the date type of a variable using type()
type(True)
or
type(123)
We can also read value from keyboard in command line using input()
city = input("what city do you live in? ")
TIP
"what city do you live in? "
is an argument of input()
, it displays as a prompt in the command line to us.
We will learn more about arguments of a function in future section.
Check out our response by
print(city)
WARNING
Remember that input()
always gives value in string
# Assignment 1
Write some Python statements to ask our names then display "Hello, name
".
The workflow the program is as follows:
First, the command line would prompt "What is your name? "
Suppose we enter "Jack".
Then, the command line displays "Hello, Jack"
Sample Solution
name = input("What is your name? ")
print("Hello,", name)
2
3