Python_2
Python_2
Wadhah S. Sebti
Running Code
>>> 2 + 2
The output is:
4
Syntax
Python is known for its clean syntax.
Semicolons:
def my_function():
print('First command’)
name = ‘Ali’
Name = ‘Mona'
Syntax
Comments
A = “Ali”
print(A)
Comments
Comments are any text to the right of the # symbol and is
mainly useful as notes for the reader of the program.
For example:
#calculates the sum of any given two numbers
a+b
Print('hello world’) # Note that print is a
statement
Data Types
• In programming, a data type consists of a set of
values and a set of operations that can be performed
on those values. A literal is the way a value of a data
type looks to a program.
Data Types
• Python interpreter evaluates a literal, the value it
returns is simply that literal. Table 2-2
The backslash symbol (\) signifies that the character that follows it is
a control code, not a literal character.
Control Codes (Escape Sequences)
Control Codes within Strings
The \n control code represents the newline control code which moves
the text cursor down to the next line in the console window.
\t for tab,
\b for backspace,
print('1\a2\a3\a4\a5\a6')
Control Codes within Strings
print("Did you know that 'word' is a word?")
age=32
print(name)
print(age)
Variables
In summary, variable names:
• Are Case sensitive: time and TIME are not the same
print(x)
x = 20
the output:
x = 100 y = -45 z = 0
Types
Determining the Type
• Just use the type() function and pass the variable of your choice as
an argument, like the example below.
type(my_variable)
print(type(my_variable))
Types
Boolean:
my_bool = True
print(type(my_bool))
<class 'bool'>
Types
Numbers
• Integer
my_int = 32
print(type(my_int))
<class 'int'>
Types
Numbers
• Float
my_float = 32.85
print(type(my_float))
<class 'float'>
String Concatenation
• String
You can join two or more strings to form a new string using the
concatenation operator +
.Here is an example:
word2 = 'York'
print(word1 + word2)
Output = NewYork