Uploaded by Wat20 sit20

Lesson 1

advertisement
A high level
developed by
1991.
Python
programming
Guido van Rossum
in
Has a very clean syntax and let
you work more quickly by writing
fewer lines of codes.
A
simple
python
>>> a = “hello world”
>>> b = 2
>>> c = 4
>>> print(a)
>>> print(b + c)
OUTPUT:
hello world
6
program:
Let
“a”
be
3
a = 3
Variables and datatypes
●
●
●
https://realpython.com/python-data-types/
Variables cannot start with a
number.
They cannot contain
spaces.Separate with underscores
instead.
Capital letters “should” not be
used.
Basic data types
●
integer
●
float
●
(complex numbers)
●
string
●
boolean
>>> apple = “hello”
>>> Apple = “hello”
>>> app le = “hello”
Examples
>>> app_le = “hello”
>>> __apple__ = “hello”
######################
>>> a = “Hello”
>>> b = 3
>>> c = 3.0
>>> x = “1”
>>> banana = True
>>> apple = “False”
Simple arithmetic
Operators
●
●
●
●
●
●
Addition (+)
Subtraction (-)
Multiplication
Division (/)
Modulus (%)
Exponent (**)
operators
(*)
Comparison operators
●
Equal to ( == )
●
Not equal to (!=)(<>)
●
Greater than (>)
●
Less than (<)
●
Greater than or equal to (>=)
●
Less than or equal to (<=)
>>> x = 3
>>> y = 6
>>> print(x + y)
Operations on data types
You can perform operations on the data types using the
arithmetic operators.
OUTPUT:
9
>>>
>>>
>>>
>>>
x = “hel”
y = “lo”
z = x + y
print(z)
OUTPUT:
Hello
Play around with other operators.
Order of operations
Don’t care. Use
parentheses.
-
Comments
Starts
with
a
“#”
token
>>> print(“hello”)
>>> #this is a comment
Intended for other programmers. Help
explain the code.
>>> #print(“hello”)
>>> print(“the above line did nothing”)
OUTPUT:
Hello
the above line did nothing
-
Functions
- input()
- print()
- type()
These are built-in functions
in Python. Later, you will
be able to write your own
functions too!
>>> print(“hello”)
OUTPUT:
Hello
>>> print(type(“hello”)
OUTPUT:
<class 'str'>
>>> x = input(“Who are you?”)
OUTPUT:
Who are you?James
Type convertors functions
-
str()
int()
float()
etc.
Values of a certain data
type can be converted into
another.(*there are
exceptions)
>>> a = “1”
>>> print(type(a))
>>> print(type(int(a)))
OUTPUT:
<class 'str'>
<class 'int'>
>>> a = 1
>>> print(float(a))
OUTPUT:
1.0
Download