0% found this document useful (0 votes)
14 views11 pages

Python Qazaqsha Sabak 4.1

The document discusses Python functions and procedures. It shows how to define functions with and without parameters and return values. It also demonstrates how to call functions and pass arguments. Various function examples are provided like printing text a certain number of times or calculating the sum of integers up to a number. Recursion and importing math/random modules are also covered.

Uploaded by

Damir Muratbaev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

Python Qazaqsha Sabak 4.1

The document discusses Python functions and procedures. It shows how to define functions with and without parameters and return values. It also demonstrates how to call functions and pass arguments. Various function examples are provided like printing text a certain number of times or calculating the sum of integers up to a number. Recursion and importing math/random modules are also covered.

Uploaded by

Damir Muratbaev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

def - Процедура

In [1]: a = 10
b = 5
print(a+b)

15

In [2]: def kosu(a,b): # a,b - параметрлер


print(a+b)

kosu(10,5)

15

In [3]: s = input('Atynyz kim bolady? ')


print(f'Tanyskanyma kuanyshtymyn, {s}!')

Atynyz kim bolady? Yergali


Tanyskanyma kuanyshtymyn, Yergali!

In [4]: def tanysu(s):


print(f'Tanyskanyma kuanyshtymyn, {s}!')

tanysu(input('Atynyz kim bolady? '))

Atynyz kim bolady? Yergali


Tanyskanyma kuanyshtymyn, Yergali!

In [5]: def salemdesu():


s = 'Salem! '*3
print(s)

salemdesu()
salemdesu()

Salem! Salem! Salem!


Salem! Salem! Salem!

In [6]: def salemdesu(n = 3):


s = 'Salem! '*n
print(s)

salemdesu()
salemdesu(10)

Salem! Salem! Salem!


Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem!

In [7]: def salemdesu(n = 3, name = 'Dosym'):


s = 'Salem! '*n
s+=name+'!'
print(s)

salemdesu()
salemdesu(1)
salemdesu(1,'Yergali')

Salem! Salem! Salem! Dosym!


Salem! Dosym!
Salem! Yergali!
In [8]: a = 5
n = 10
total = 0
for i in range(n):
total+=i*a
print(f'i*a = {i}*{a} = {i*a}')
print(total)

i*a = 0*5 = 0
i*a = 1*5 = 5
i*a = 2*5 = 10
i*a = 3*5 = 15
i*a = 4*5 = 20
i*a = 5*5 = 25
i*a = 6*5 = 30
i*a = 7*5 = 35
i*a = 8*5 = 40
i*a = 9*5 = 45
225

In [9]: def kobeitu_sum(a,n):


total = 0
for i in range(n):
total+=i*a
print(f'i*a = {i}*{a} = {i*a}')
print(total)

kobeitu_sum(5,10)

i*a = 0*5 = 0
i*a = 1*5 = 5
i*a = 2*5 = 10
i*a = 3*5 = 15
i*a = 4*5 = 20
i*a = 5*5 = 25
i*a = 6*5 = 30
i*a = 7*5 = 35
i*a = 8*5 = 40
i*a = 9*5 = 45
225

def return - Функция


In [10]: def kobeitu_sum(a,n):
total = 0
for i in range(n):
total+=i*a
print(f'i*a = {i}*{a} = {i*a}')
return total

a = kobeitu_sum(5,10)
print(a)

i*a = 0*5 = 0
i*a = 1*5 = 5
i*a = 2*5 = 10
i*a = 3*5 = 15
i*a = 4*5 = 20
i*a = 5*5 = 25
i*a = 6*5 = 30
i*a = 7*5 = 35
i*a = 8*5 = 40
i*a = 9*5 = 45
225
In [11]: def kobeitu_sum(a,n):
total = 0
for i in range(n):
total+=i*a
print(f'i*a = {i}*{a} = {i*a}')
return total

a = kobeitu_sum(5,10)
b = kobeitu_sum(a,10)
print(b)

i*a = 0*5 = 0
i*a = 1*5 = 5
i*a = 2*5 = 10
i*a = 3*5 = 15
i*a = 4*5 = 20
i*a = 5*5 = 25
i*a = 6*5 = 30
i*a = 7*5 = 35
i*a = 8*5 = 40
i*a = 9*5 = 45
i*a = 0*225 = 0
i*a = 1*225 = 225
i*a = 2*225 = 450
i*a = 3*225 = 675
i*a = 4*225 = 900
i*a = 5*225 = 1125
i*a = 6*225 = 1350
i*a = 7*225 = 1575
i*a = 8*225 = 1800
i*a = 9*225 = 2025
10125

In [12]: def s(esim='Bot',jas = 1):


return f'Salem! menim esim - {esim}, menim jasym - {jas}'

a = s()
print(a)

Salem! menim esim - Bot, menim jasym - 1

In [13]: def s(esim='Bot',jas = 1):


return f'Salem! {esim}! Senin jasyn - {jas}'

a = s()
print(a)
print(s(a))
print(s(s(a)))

Salem! Bot! Senin jasyn - 1


Salem! Salem! Bot! Senin jasyn - 1! Senin jasyn - 1
Salem! Salem! Salem! Bot! Senin jasyn - 1! Senin jasyn - 1! Senin jasyn - 1

In [14]: # jai sandar: ozine gana bolinetin sandar 2,3,5,...


for i in range(2,100):
a=i
k=0
for j in range(2,a):
if(a%j==0):
k+=1
if(k==0):
print(a,end=' ')

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
In [15]: def is_prime(n):
if n == 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True

print(is_prime(97))

for i in range(1,100):
if is_prime(i):
print(i,end=' ')

True
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Рекурсия - Динамическое программирование


In [16]: n = 0
for i in range (1, 11):
n += i
print(n)

55

In [17]: def kosu(n):


if n == 1:
return 1
else:
return n + kosu(n - 1)

kosu(10)

Out[17]: 55

In [18]: def factorial(n):


if n == 0:
return 1
else:
return n * factorial(n - 1)

print(factorial(10))

for i in range(11):
print(factorial(i),end=' ')

3628800
1 1 2 6 24 120 720 5040 40320 362880 3628800

In [19]: def fib(n):


if n == 0 or n == 1:
return n
else:
return fib(n - 2) + fib(n - 1)

print(fib(10))

for i in range(11):
print(fib(i),end=' ')

55
0 1 1 2 3 5 8 13 21 34 55
In [20]: def is_prime(n):
if n == 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True

def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n - 2) + fib(n - 1)

# fibannochi retindegi san jai san bola ala ma?


for i in range(1,20):
a = fib(i)
if is_prime(a):
print(a)

2
3
5
13
89
233
1597

import math
In [21]: import math
print(math.pi)
print(math.e)

3.141592653589793
2.718281828459045

In [22]: print(math.sqrt(25))

5.0

In [23]: print(math.factorial(5))

120

In [24]: from math import *


print(pi)
print(e)
print(sqrt(25))
print(factorial(5))

3.141592653589793
2.718281828459045
5.0
120

In [25]: print(gcd(63,27)) # En ulken Ortak BOlgish (EUOB)

In [26]: print(63*27//gcd(63,27)) # En kishi ortak eselik (EKOE)

189
In [27]: a = sqrt(24)
print(a)
print(round(a))
print(ceil(a))

4.898979485566356
5
5

In [28]: a = sqrt(17)
print(a)
print(round(a))
print(ceil(a))

4.123105625617661
4
5

In [29]: print(e)
print(exp(1))
print(exp(10))

2.718281828459045
2.718281828459045
22026.465794806718

In [30]: print(log(2.718281828459045))

1.0

In [31]: print(pow(2,5))
print(pow(10,3))

32.0
1000.0

In [32]: print(log2(32))
print(log10(1000))

5.0
3.0

In [33]: print(log(32,2))
print(log(1000,10))

5.0
2.9999999999999996

import random
In [34]: import random
print(random.randint(1,100))

68

In [35]: from random import randint as r


print(r(1,100))

13

In [36]: random.seed(100)
print(r(1,100))

19
In [37]: #random.seed(100)
a = r(1,100)

if a == 19:
print('Durys!')
else:
print('Kate!')

Kate!

In [38]: from random import *


a = ['A','1','ad','2345',34,-543,0.5,-3456.34]
choice(a)

Out[38]: -3456.34

In [39]: print(random())

0.9517177028267368

In [40]: seed(1)
print(random())

0.13436424411240122

File I/O - input/output


In [41]: f = open("file1.txt",mode='r') # r - read
print(f.read())
f.close()

Salem Alem!
Bul text-file!
Faildarmen jumys isteu.

In [42]: f = open("file2.txt",mode='w') # w - write


f.write("Jana text jazu!\n")
f.write("Baska file.\n")
f.close()

In [43]: f = open("file2.txt",mode='r')
print(f.read()) # read
f.close()

Jana text jazu!


Baska file.

In [44]: f = open("file2.txt",mode='r')
print(f.readline()) # readline
f.close()

Jana text jazu!

In [45]: f = open("file2.txt",mode='a') # a - append


f.write("123+1234=?\n")
f.write("1000*234=?\n")
f.close()

In [46]: f = open("file2.txt",mode='r')
print(f.read())
f.close()

Jana text jazu!


Baska file.
123+1234=?
1000*234=?
read/readline

In [47]: file_input = open("input.txt")


file_output = open("output.txt","w")

a, b = map(int, file_input.readline().split())
print(a,b)
c = a+b
print(c)
file_output.write(str(c))

file_input.close()
file_output.close()

123 678
801

In [48]: file_input = open("input1.txt")


file_output = open("output.txt","w")

a, b = map(int, file_input.read().split())
print(a,b)
c = a+b
print(c)
file_output.write(str(c))

file_input.close()
file_output.close()

123 456
579

In [49]: file_input = open("input2.txt")


file_output = open("output.txt","w")

a = list(map(int, file_input.read().split()))
print(a)
c = sum(a)
print(c)
file_output.write(str(c))

file_input.close()
file_output.close()

[123, 345, 234, 12, 345, 5432, 23]


6514

with open

In [50]: with open("input1.txt") as file_input:


a, b = map(int, file_input.read().split())

with open("output.txt",'w') as file_output:


file_output.write(str(a+b))

print(a,b)
print(a+b)

123 456
579

қысқа жазу

if/else
In [51]: a = 10
if a > 0:
s = 'Ong san'
else:
s = 'Teris san'
print(s)

Ong san

In [52]: a = 10
s = 'Ong san' if a > 0 else 'Teris san'
print(s)

Ong san

In [53]: a = 0
if a > 0:
s = 'Ong san'
elif a < 0:
s = 'Teris san'
else:
s = '0-ge ten'
print(s)

0-ge ten

In [54]: a = 0
s = 'Ong san' if a>0 else 'Teris san' if a<0 else '0-ge ten'
print(s)

0-ge ten

In [55]: a,b = 63,9


if a%b==0:
print('YES')
else:
print('NO')

YES

In [56]: a,b = 63,9


print(a%b and 'NO' or 'YES')

YES

for loop

In [57]: for i in range(10):


print(i,end=' ')

0 1 2 3 4 5 6 7 8 9

In [58]: print(*[i for i in range(10)])

0 1 2 3 4 5 6 7 8 9

In [59]: a = []
for i in range(10):
if i%2==1:
a.append(i*2)
print(a)

[2, 6, 10, 14, 18]

In [60]: print([i*2 for i in range(10) if i % 2 == 1])

[2, 6, 10, 14, 18]


In [61]: a = [1, 2, 3, 4]
print(*a)
print(*a, sep=', ')
print(*a, sep=' + ')

1 2 3 4
1, 2, 3, 4
1 + 2 + 3 + 4

lambda

In [62]: def kosu(x):


return x**2
print(kosu(10))

100

In [63]: kosu = lambda x: x ** 2


print(kosu(10))

100

In [64]: def salemdesu(n = 3):


s = 'Salem! '*n
print(s)

salemdesu()
salemdesu(10)

Salem! Salem! Salem!


Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem!

In [65]: salemdesu = lambda n=3: 'Salem! '*n


print(salemdesu())
print(salemdesu(10))

Salem! Salem! Salem!


Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem! Salem!

In [66]: a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = []
for i in range(len(a)):
c.append(a[i]+b[i])
print(*c)

7 9 11 13 15

In [67]: a = [1,2,3,4,5]
b = [6,7,8,9,10]
print(*map(lambda x,y:x+y, a,b))

7 9 11 13 15

In [68]: P = print
P('Salem')

Salem

In [69]: I = input
s=I()
P(s)

Salem Alem!
Salem Alem!

Осы өткен материалдарды тексеру ¶


http://tiny.cc/Sabaq4 (http://tiny.cc/Sabaq4)

You might also like