Execute Python
Indentation
Python uses indentation for blocks, instead of curly braces. Both tabs and
spaces are supported, but the standard indentation requires standard Python
code to use four spaces. You have to use the same number of spaces in the same block of
code, otherwise Python will give you an error: For example:
x=1
if x == 1:
# indented four spaces
print("x is 1.")
Python Variables
Variables are containers for storing data values. In Python, variables are created
when you assign a value to it. Python has no command for declaring a variable.
Variable names are case-sensitive.
Just create a variable and assign it any value you want and then use the print
function to print it
Eg
myNumber = 3
print(myNumber)
Rule of naming a variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables)
A variable name cannot be any of the Python keywords.
Multiple Assignment
x=y=z=50 # Assigning single value to
multiple variables
a,b,c=5,10,15 # Assigning multiple values to multiple variables:
Delete a variable
We can delete the variable using the del keyword.
Syntax : del <variable_name>
Python Data Types
Variables can store data of different types, and different types can do different
things. Some of Python data types are
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: Bool
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Getting the Data Type
You can get the data type of any object by using the type () function:
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor
functions:
x = str("Hello World")
Python Numbers
x = 1 # int
y = 2.8 # float
z = 1j # complex
Int
Int, or integer, is a whole number, positive or negative, without decimals, of
unlimited length.
x = 1
y = 35656222554887711
z = -3255522
Float
Float, or "floating point number" is a number, positive or negative, containing
one or more decimals.eg
x = 1.10
y = 1.0
z = -35.59
Float can also be scientific numbers with an "e" to indicate the power of 10.eg
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
String
A string is a collection of one or more characters put in a single quote, double-
quote, or triple-quote. Triple-quote for multiline string.Eg
String1 = 'Welcome to the programming World'
print(String1)
List Data Type
which is an ordered collection of data
Creating List
Lists in Python can be created by just placing the sequence inside the square
brackets[].
List = ["Ethio", "For", "ethio"]
Or
List = list(("ethio", "For", "ethio"))
Python Access List Items
In order to access the list items refer to the index number. Use the index
operator [ ] to access an item in a list
Python Comments
Comments are to clarify code and are not interpreted by Python, they may be omitted
when typing in examples. Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
#This is a comment
print("Hello, World!")
Escape means to remove special meaning from a character so it can be used as it can be used
literally.