0% found this document useful (0 votes)
8 views

Python_2

Uploaded by

khaled9khaled999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python_2

Uploaded by

khaled9khaled999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Python

Wadhah S. Sebti
Running Code

You can run Python code directly in


the terminal as commands or you
can save the code in a file with the
.py extension and run the Python
file.
Running Code
Terminal:

Running commands directly in the terminal is


recommended when you want to run something
simple.

Open the command line and type python3:


C:>python3
Running Code
Terminal:
C:>python3
>>>
Let's test it by running our first program to perform basic math and add
two numbers.

>>> 2 + 2
The output is:
4
Syntax
Python is known for its clean syntax.

The language avoids using unnecessary characters to indicate some


specificity.

Semicolons:

• Python makes no use of semicolons to finish lines. A new line is


enough to tell the interpreter that a new command is beginning.
Syntax
Indentation

• Many languages use curly-brackets to define scopes.

• Python's interpreter uses only indentation to define when a


scope ends and another one starts.

• This means you have to be aware of white spaces at the


beginning of each line.
Syntax
Indentation

This definition of a function works:

def my_function():

print('First command’)

This doesn't work because the indentation of the second line is


missing and will throw an error:
def my_function():
print('First command')
Syntax
Case sensitivity and variables
Python is case sensitive. So the variables name and
Name are not the same thing and store different values.

name = ‘Ali’

Name = ‘Mona'
Syntax
Comments

to comment something in your code, use the hash mark #. The


commented part does not influence the program flow.

# this will print Ali’s name

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

• shows example literals of several Python data types.


Data Types
• String

In Python, a string literal is a sequence of characters


enclosed in single or double quotation marks. The following
session with the Python shell shows some example strings:
Data Types
• String
>>> 'Hello there!'
'Hello there!'
>>> "Hello there!"
'Hello there!'
>>> ''
''
>>> ""
Data Types
• String
>>> ''
''
>>> ""
''
The last two string literals (‘ ’ and "“ ) represent the
empty string. Although it contains no characters, the
empty string is a string nonetheless.
Data Types
• String
>>> "I'm using a single quote in this string!"
"I'm using a single quote in this string!"
>>> print("I'm using a single quote in this string!")
I'm using a single quote in this string!
Control Codes within Strings
The characters that can appear within strings include letters of the
alphabet (A-Z, a-z), digits (0-9), punctuation (., :, ,, etc.), and other
printable symbols (#, &, %, etc.). In addition to these “normal”
characters, we may embed special characters known as control
codes or Escape Sequences.

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,

\a for alert (or bell).

The \b and \a do not produce the desired results in the interactive


shell, but they work properly in a command shell.
Control Codes within Strings
print('A\nB\nC') the output:
A
B
print('D\tE\tF') C
DEF
print('WX\bYZ') WYZ
123456

print('1\a2\a3\a4\a5\a6')
Control Codes within Strings
print("Did you know that 'word' is a word?")

print('Did you know that "word" is a word?')

print('Did you know that \'word\' is a word?')

print("Did you know that \"word\" is a word?")


the output:
Did you know that 'word' is a word?
Did you know that "word" is a word?
Did you know that 'word' is a word?
Did you know that "word" is a word?
Variables
• In any program, you need to store and manipulate data to
create a flow or some specific logic.

• That's what variables are for.

• You can have a variable to store a name, another one to


store the age of a person, or even use a more complex type
to store all of this at once like a dictionary.
Variables
name='Bob'

age=32

You can use the print() function to show the value of a


variable.

print(name)

print(age)
Variables
In summary, variable names:

• Are Case sensitive: time and TIME are not the same

• Have to start with an underscore _ or a letter (DO NOT start with a


number)

• Are allowed to have only numbers, letters and underscores. No


special characters like: #, $, &, @, etc.

• This, for instance, is not allowed: party#time, 10partytime.


Variables and Assignment
x = 10

This is an assignment statement. An assignment statement


associates a value with a variable. The key to an assignment
statement is the symbol = which is known as the assignment
operator.

It is best to read x = 10 as “x is assigned the value 10,”

or “x gets the value 10.”


Variables and Assignment
x = 10

print(x)

x = 20

print(x) the print statements produce different results:


10
x = 30 20
30
print(x)
Variables and Assignment
x = 10
uses the str function to treat x as a
print('x = ' + str(x)) string so the + operator will use
string concatenation:
x = 20 print('x = ' + str(x))
print('x = ' + str(x)) the output:
x = 10
x = 30 x = 20
x = 30
print('x = ' + str(x))
Variables and Assignment
x = 10
illustrates the print function
print('x = ‘ , x) accepting two parameters. The first
parameter is the string 'x =', and the
x = 20 second parameter is the variable x
bound to an integer value.
print('x = ‘ , x)
the output:
x = 30 x = 10
x = 20
print('x = ‘ , x) x = 30
Variables and Assignment
A programmer may assign multiple variables in one
statement.
x, y, z = 100, -45, 0
print('x =', x, ' y =', y, ' z =', z)

the output:
x = 100 y = -45 z = 0
Types
Determining the Type

• First of all, let's learn how to determine the data 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:

A boolean type variable can only represent either True or


False.

my_bool = True

print(type(my_bool))

<class 'bool'>
Types
Numbers

There are three numeric types: int, float, and complex.

• 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:

>>> "Hi " + "there, " + "Ken!"

'Hi there. Ken!'


String Concatenation
• String

word1 = 'New '

word2 = 'York'
print(word1 + word2)
Output = NewYork

You might also like