back

Lecture 02

In [1]:
a=5
In [2]:
if a>3:
    print("a>3 is true")
else:
    print("a>3 is false")
a>3 is true
In [3]:
a=1.3
if a>1:
    if a>2:
        print("a>2")
    else:
        print("1<a<2")
else:
    print("a<1")
1<a<2
In [4]:
a=4
if a/2==a//2:
    print("a==2n")
else:
    print("a!=2n")
a==2n
In [5]:
a=6
if a/2==a//2 and a/3==a//3:
    print("ok")
else:
    print("rong!")
ok
In [6]:
a=6
if a/2==a//2 or a/3==a//3:
    print("ok")
else:
    print("rong!")
ok
In [7]:
name='reza'
if name=='Ali':
    print("salam",name)
elif name=='Hasan':
    print("byby ",name)
else:
    print("nothing.")
nothing.

$ax^2+bx+c$

In [8]:
a=-3
b=2
c=-2
delta=b**2-4*a*c
if delta>0:
    print("ریشه حقیقی",delta)
elif delta<0:
    print("ریشه مختلط",delta)
else:
    print("ریشه مضاعف",delta)
ریشه مختلط -20
In [9]:
number = 23
guess = int(input('Enter an integer : '))
if guess == number:
   print('Congratulations, you guessed it.') # New block starts here
   print("(but you do not win any prizes!)") # New block ends here
elif guess < number:
   print('No, it is a little higher than that') # Another block
   # You can do whatever you want in a block ...
else:
   print('No, it is a little lower than that')
   # you must have guess > number to reach here
print('Done')
   # This last statement is always executed, after the if statement is executed
Enter an integer : 20
No, it is a little higher than that
Done
In [10]:
guess = int(input('Enter an integer : '))
if guess%2 == 0:
    print('x=2c') # New block starts here
else:
    print('x!=2c') # New block starts here
Enter an integer : 5
x!=2c
In [11]:
x = int(input('Enter an integer 1<x<10 : '))
#1 2 3 5 8
if x == 1:
    print('ok') # New block starts here
elif x==2:
    print('rong') # New block starts here
Enter an integer 1<x<10 : 5
In [12]:
#!/usr/bin/python
# Filename: if.py
a= input('Enter a number : ')
afloat=float(a)
aint=int(afloat)
if aint==afloat:
    print('It is an integer number') # New block starts here
else:
    print('It is a float number') # New block starts here
Enter a number : 2.3
It is a float number
In [13]:
x=1.236487979646
n=4
x=int(x*10**n)
x=x/10**n
print(x)
1.2364
In [14]:
x=1.236487979646
sx=str(x)
print(sx)
1.236487979646
In [15]:
type(x)
Out[15]:
float
In [16]:
type(sx)
Out[16]:
str
In [17]:
txt='salam ali.'
In [18]:
print(txt)
salam ali.
In [19]:
txt[0]
Out[19]:
's'
In [20]:
txt[5]
Out[20]:
' '
In [21]:
s=txt[0:-2]
print(s)
salam al
In [22]:
x=10.236487979646
n=2
sx=str(x)
print(sx[0:2+1+n])
10.23