python programming basic syntax
This lecture gives you a theoretical understanding of python's basic syntax: The recorded video lecture can be found below. The link to the Jupyter notebook for you to practice can the found here:https://notebooksharing.space/view/706d250ebc784f26936e1c52055eb0e4dffeca51cfb1912216e86b24c0ac4370#displayOptions= # Python Basic syntax:
(copy the link and paste it to your browser) Enjoy!
What you will learn at the end of this lecture?
What is python?
commenting in python
Variables
Data types
Operators
Control flow
What is python?
Python is a high-level, interpreted programming language. It is intended to be easy to read and write, as well as highly readable. Python's syntax is simple and straightforward, and it employs indentation to denote code blocks.
Comments in python
The # symbol in Python is used to add comments to your code. The interpreter ignores everything after the # symbol on a line, so you can use it to add notes or explanations to your code.
# Let’s take a look at this example
# This is a comment
x = 5 # This is also a comment
Variables
Variables can be used to store values in Python. To make a variable, simply name it and assign it a value with the = operator.
# For instance
# store the value 5 in x
x = 5
# store the value 10 in y
y = 10
# Store the string "hello" as z
z = "hello"
You can also assign multiple variables at once
# For instance
x, y, z = 5, 10, "hello"
Data types
Python has a number of built-in data types, such as integers, floating-point numbers, and strings.
# integers
x = 5
y = 10
# floating-point numbers
x = 5.0
y = 10.5
# strings
x = "hello"
y = 'world'
We can also use the type() function to check the data type of a value
# let’s see how
x = 5
print(type(x)) # prints "<class 'int'>"
x = 5.0
print(type(x)) # prints "<class 'float'>"
x = "hello"
print(type(x)) # prints "<class 'str'>"
Operators
Python has several built-in operators for performing operations on values. Some common operators are:
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulus (remainder of division)
**: Exponentiation
>: greater than
<: less than
>=: greater than or equal to
!= : not equal
# Let’s look at a few examples
x = 5 + 2 # 7
y = 5 - 2 # 3
z = 5 * 2 # 10
a = 5 / 2 # 2.5
b = 5 % 2 # 1
c = 5 ** 2 # 25
Control Flow
Python uses indentation to denote code blocks. The amount of indentation is up to you, but four spaces per level of indentation are recommended.
# Ok, take a look.....
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
The "if" statements can be used to control the flow of your code. You can also use elif (short for "else if") to add more conditions and else to specify a block of code to run if none of the conditions is met.
# Ok, let’s look at this example
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
else:
print("x is equal to 5")
This concludes today's lecture; understanding the fundamentals is essential for advanced topics...