Python Introduction Lab 1
Python Introduction Lab 1
C S S C C + + C # B O O T S T R A P R E A C T M Y S Q L J Q U E R Y E X C E L X M L D J A N G O N O D E J S R T Y P E S C R I P T A N G U L A R G I T P O S T G R E S Q L M O N G O D B A S P A W S A I G O K O T L I N S A S S V U E G E N A I C Y B E R S E C U R I T Y D A T A S C I E N C E
Python Introduction
What is Python?
Python is a popular programming language. It
was created by Guido van Rossum, and released
in 1991.
It is used for:
Why Python?
• Python works on different platforms
(Windows, Mac, Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the
English language.
• Python has syntax that allows developers to
write programs with fewer lines than some
other programming languages.
• Python runs on an interpreter system,
meaning that code can be executed as
soon as it is written. This means that
prototyping can be very quick.
• Python can be treated in a procedural way,
an object-oriented way or a functional
way.
Python Comments
Example
#This is a comment
print("Hello, World!")
Example
print("Hello, World!") #This is a comment
A comment does not have to be text that explains the code, it can also be
used to prevent Python from executing code:
Example
#print("Hello, World!")
print("Cheers, Mate!")
Multiline Comments
Python does not really have a syntax for multiline comments.
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Or, not quite as intended, you can use a multiline string.
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your
comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
As long as the string is not assigned to a variable, Python will read the code,
but then ignore it, and you have made a multiline comment.
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
Example
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even
change type after they have been set.
Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Casting
If you want to specify the data type of a variable, this can be done with
casting.
Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Example
x = 5
y = "John"
print(type(x))
print(type(y))
Example
x = "John"
# is the same as
x = 'John'
Case-Sensitive
Variable names are case-sensitive.
Example
This will create two variables:
a = 4
A = "Sally"
#A will not overwrite a
Variable Names
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). Rules for Python variables:
Example
Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Example
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Multi Words Variable Names
Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John"
Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables
The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Example
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Example
x = 5
y = 10
print(x + y)
In the print() function, when you try to combine a string and a number with
the + operator, Python will give you an error:
Example
x = 5
y = "John"
print(x + y)
Example
x = 5
y = "John"
print(x, y)
Tasks:
This is a comment
2.Create a variable named carname and assign the value Volvo to it.
2my-first_name = "John"
6.The following code example would print the data type of x, what data type
would that be?
x = 5
print(type(x))
7.The following code example would print the data type of x, what data type
would that be?
x = "Hello World"
print(type(x))
x = 5.5
x = ? (x)
This is a comment
written in
more than just one line